Introduction to Programming with C++, Third Edition, Y. Daniel Liang

This quiz is for students to practice. A large number of additional quiz is available for instructors using Quiz Generator from the Instructor's Resource Website. Videos for Java, Python, and C++ can be found at https://yongdanielliang.github.io/revelvideos.html.

Many questions in this edition have been updated in the new edition. Please check with the publisher on the newest edition.

Chapter 16 Exception Handling


Section 16.2 Exception-Handling Overview
16.1  Which of the following statements are correct?
A. The execution of a throw statement is called throwing an exception.
B. You can throw a value of any type.
C. The try block contains the code that are executed in normal circumstances.
D. The catch block contains the code that are executed when an exception occurs.

16.2  What is wrong in the following code?

  vector<int> v;
  v[0] = 2.5;
A. The program has a compile error because there are no elements in the vector.
B. The program has a compile error because you cannot assign a double value to v[0].
C. The program has a runtime error because there are no elements in the vector.
D. The program has a runtime error because you cannot assign a double value to v[0].

16.3  If you are not interested in the contents of an exception object, the catch block parameter may be omitted.
A. true
B. false

16.4  If you enter 1 0, what is the output of the following code?

 #include <iostream>
using namespace std;

int main()
{
  // Read two integers
  cout << "Enter two integers: ";
  int number1, number2;
  cin >> number1 >> number2;

  try
  {
    if (number2 == 0)
      throw number1;

    cout << number1 << " / " << number2 << " is "
      << (number1 / number2) << endl;

    cout << "C" << endl;
  }
  catch (int e)
  {
    cout << "A" << endl;
  }

  cout << "B" << endl;

  return 0;
}
A. A
B. B
C. C
D. AB

16.5  catch (type p) acts very much like a parameter in a function. Once the exception is caught, you can access the thrown value from this parameter in the body of a catch block.
A. true
B. false

Section 16.4 Exception Classes
16.6  Which of the following classes are predefined in C++?
A. exception
B. runtime_error
C. overflow_error
D. underflow_error
E. bad_exception

16.7  Which of the following classes are in the header file <stdexcept>?
A. exception
B. runtime_error
C. overflow_error
D. underflow_error
E. bad_exception

16.8  Which of the following classes are in the header file <stdexcept>?
A. logic_error
B. invalid_argument
C. length_error
D. out_of_range
E. bad_cast

16.9  The function what() is defined in ______________.
A. exception
B. runtime_error
C. overflow_error
D. underflow_error
E. bad_exception

Section 16.5 Custom Exception Classes
16.10  Which of the following statements are true?
A. A custom exception class is just like a regular class.
B. A custom exception class must always be derived from class exception.
C. A custom exception class must always be derived from a derived class of class exception.
D. A custom exception class must always be derived from class runtime_error.

Section 16.6 Multiple Catches
16.11  Suppose Exception2 is derived from Exception1. Analyze the following code.

try {
  statement1;
  statement2;
  statement3;
}
catch (Exception1 ex1) 
{
}
catch (Exception2 ex2) 
{
}
A. If an exception of the Exeception2 type occurs, this exception is caught by the first catch block.
B. If an exception of the Exeception2 type occurs, this exception is caught by the second catch block.
C. The program has a compile error because these two catch blocks are in wrong order.
D. The program has a runtime error because these two catch blocks are in wrong order.

Section 16.8 Rethrowing Exceptions
16.12  Suppose that statement2 throws an exception of type Exception2 in the following statement:

try {
  statement1;
  statement2;
  statement3;
}
catch (Exception1 ex1) 
{
}
catch (Exception2 ex2) 
{
}
catch (Exception3 ex3) 
{
  statement4;
  throw;
}
statement5;

Which statements are executed after statement2 is executed?
A. statement1
B. statement2
C. statement3
D. statement4
E. statement5

16.13  Suppose that statement3 throws an exception of type Exception3 in the following statement:

try {
  statement1;
  statement2;
  statement3;
}
catch (Exception1 ex1) 
{
}
catch (Exception2 ex2) 
{
}
catch (Exception3 ex3) 
{
  statement4;
  throw;
}
statement5;

Which statements are executed after statement3 is executed?
A. statement1
B. statement2
C. statement3
D. statement4
E. statement5

Section 16.9 Exception Specification
16.14  Which of the following statements are true?
A. A function should warn the programmers that any exceptions it might throw, so the programmers can write robust program to deal with these potential exceptions in a try-catch block.
B. If a function is declared as returnType functionName(parameterList) throw (type), this function can only throw the exception of the specified type.
C. Placing throw() after a function header, known as an empty exception specification, declares that the function does not throw any exceptions.
D. Throwing an exception that is not declared in the throw list will causes function unexpected to be invoked.
E. A function without exception specification can throw any exception and will not cause unexpected to be invoked.

Section 16.10 When to Use Exceptions
16.15  Which of the following statements are true?
A. C++ allows you to throw a primitive type value or any object-type value.
B. In general, common exceptions that may occur in multiple classes in a project are candidates for exception classes.
C. Simple errors that may occur in individual functions are best handled locally without throwing exceptions.
D. Exception handling is for dealing with unexpected error conditions. Do not use a try-catch block to deal with simple, expected situations.