Introduction to C++ Programming, Y. Daniel Liang
Chapter 14 Exception Handling
Section 14.2 Exception-Handling Overview
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.
2
What is wrong in the following code?
   vector<int> v;
   v[0] = 2.5;
A.
The program has a syntax error because there are no elements in the vector.
B.
The program has a syntax 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].
3
If you are not interested in the contents of an exception object, the catch block parameter may be omitted.
A.
true
B.
false
4
If you enter 1 0, what is the output of the following code?
  #include <iostream>
using namespace std;
int main()
{
   // Read two intergers
   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
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 14.4 Exception Classes
6
Which of the following classes are predefined in C++?
A.
exception
B.
runtime_error
C.
overflow_error
D.
underflow_error
E.
bad_exception
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
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
9
The function what() is defined in ______________.
A.
exception
B.
runtime_error
C.
overflow_error
D.
underflow_error
E.
bad_exception
Section 14.5 Custom Exception Classes
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 14.6 Multiple Catches
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 syntax 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 14.8 Rethrowing Exceptions
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
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 14.9 Exception Specification
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 14.10 When to Use Exceptions
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.