CSC161 Ch14 HW

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

a. { cout << "Enter the balance: "; cin >> balance; cout << endl; if (balance < 1000.00) throw balance; cout << "Leaving the try block." << endl; } b. catch (double x) { cout << "Current balance: " << x << endl << "Balance must be greater than 1000.00" << endl; } c. double x d. if (balance < 1000.00) throw balance;

Consider the following C++ code: double balance; try { cout << "Enter the balance: "; cin >> balance; cout << endl; if (balance < 1000.00) throw balance; cout << "Leaving the try block." << endl; } catch (double x) { cout << "Current balance: " << x << endl << "Balance must be greater than 1000.00" << endl; } a. In this code, identify the try block. b. In this code, identify the catch block. c. In this code, identify the catch block parameter and its type. d. In this code, identify the throw statement.

Inside the exception class, the default and/or argument constructor is defined and the methods returning the error messages are defined.

If you define your own exception class, what is typically included in that class?

1. try catch blocks 2. using inbuilt Exception classes 3. using user defined exception classes

Name three exception-handling techniques.

a. message = "myException thrown!"; cout << "Immediate attention required!" << endl; b. cout << "Attention required!" << endl

Suppose the exception class myException is defined as follows: class myException { public: myException() { message = "myException thrown!"; cout << "Immediate attention required!" << endl; } myException(string msg) { message = msg; cout << "Attention required!" << endl; } string what() { return message; } private: string message; } Suppose that in a user program, the catch block has the following form: catch (myException mE) { cout << mE.what() << endl; } a. What output will be produced if the exception is thrown with the default constructor? b. Also, what output will be produced if the exception is thrown with the constructor with parameters with the following actual parameter? "May Day, May Day"

try block is used to enclose the code that can throw an exception and catch block is used to handle the exception thrown by try block. We can have multiple catch blocks to handle different types of exceptions associated with one try block.

What is the difference between a try block and a catch block?


Ensembles d'études connexes

Accounting 2011 Test 1 Study Guide

View Set

Unit Two: Questions and Concepts

View Set

Discontinuities, ES Content 2 Earth Interior

View Set

ATI Video Case Study: Urinary Tract Infections.

View Set

Electrical Voltage and Current Concepts

View Set

Psychology LearningCurve: Chapter 4

View Set

Chapter 4: Genetic and Genomic Concepts for Medical-Surgical Nursing

View Set

Genetics: Differentiate between proto-oncogenes and tumor suppressor genes

View Set

Of Mice and Men Ch. 2 Study Guide

View Set