Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

Problem 1

Mark the following statements as true or false. a. The order in which catch blocks are listed is not important. b. An exception can be caught either in the function where it occurred or in any of the functions that led to the invocation of this method. c. One way to handle an exception is to print an error message and exit the program. d. All exceptions need to be reported to avoid compilation errors.

Problem 2

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

Problem 3

What will happen if an exception is thrown but not caught?

Problem 4

What happens if no exception is thrown in a try block?

Problem 6

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.

Problem 8

Consider the following C++ code: int lowerLimit; . . . try { cout << "Entering the try block." << endl; if (lowerLimit < 100) throw exception("Lower limit violation."); cout << "Exiting the try block." << endl; } catch (exception eObj) { cout << "Exception: " << eObj.what() << endl; } cout << "After the catch block" << endl; What is the output if: a. The value of lowerLimit is 50? b. The value of lowerLimit is 150?

Problem 9

Consider the following C++ code: int lowerLimit; int divisor; int result; try { cout << "Entering the try block." << endl; if (divisor == 0) throw 0; if (lowerLimit < 100) throw string("Lower limit violation."); result = lowerLimit / divisor; cout << "Exiting the try block." << endl; } catch (int x) { cout << "Exception: " << x << endl; result = 120; } catch (string str) { cout << "Exception: " << str << endl; } cout << "After the catch block" << endl; What is the output if: a. The value of lowerLimit is 50, and the value of divisor is 10? b. The value of lowerLimit is 50, and the value of divisor is 0? c. The value of lowerLimit is 150, and the value of divisor is 10? d. The value of lowerLimit is 150, and the value of divisor is 0?

Problem 10

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

Problem 11

What type of statement is used to rethrow an exception?

Problem 12

Define an exception class called tornadoException. The class should have two constructors, including the default constructor. If the exception is thrown with the default constructor, the method what should return "Tornado: Take cover immediately!". The other constructor has a single parameter, say, m, of the int type. If the exception is thrown with this constructor, the method what should return "Tornado: m miles away; and approaching!"

Access millions of textbook solutions in one place

  • Access over 3 million high quality textbook solutions
  • Access our popular flashcard, quiz, mock-exam and notes features
  • Access our smart AI features to upgrade your learning
Get Vaia Premium now
Access millions of textbook solutions in one place

Recommended explanations on Computer Science Textbooks