17.3 Rethrowing an Exception

A function might use a resource—like a file—and might want to release the resource (i.e., close the file) if an exception occurs. An exception handler, upon receiving an exception, can release the resource then notify its caller that an exception occurred by rethrowing the exception via the statement


throw;

Regardless of whether a handler can process an exception, the handler can rethrow the exception for further processing outside the handler. The next enclosing try block detects the rethrown exception, which a catch handler listed after that enclosing try block attempts to handle.

Common Programming Error 17.5

Executing an empty throw statement outside a catch handler terminates the program immediately.

Figure 17.3 demonstrates rethrowing an exception. In main’s try block (lines 25–29), line 27 calls function throwException (lines 8–21). The throwException function also contains a try block (lines 10–13), from which the throw statement in line 12 throws a Standard Library exception object. Function throwException’s catch handler (lines 14–18) catches this exception, prints an error message (lines 15–16) and rethrows the exception (line 17). This terminates the function and returns control to line 27 in the try block in main. The try block terminates (so line 28 does not execute), and the catch handler in main (lines 30–32) catches this exception and prints an error message (line 31). Since we do not use the exception parameters in this example’s catch handlers, we omit the exception parameter names and specify only the type of exception to catch (lines 14 and 30).

Fig. 17.3 Rethrowing an exception.

Alternate View

 1   // Fig. 17.3: fig17_03.cpp
 2   // Rethrowing an exception.
 3   #include <iostream>
 4   #include <exception>
 5   using namespace std;
 6
 7   // throw, catch and rethrow exception
 8   void throwException() {
 9      // throw exception and catch it immediately
10      try {
11         cout << "  Function throwException throws an exception
";
12         throw exception{}; // generate exception
13      }
14      catch (const exception&) { // handle exception
15         cout << "  Exception handled in function throwException"
16            << "
  Function throwException rethrows exception";
17          throw; // rethrow exception for further processing
18      }
19
20      cout << "This should not print
";
21  }
22
23   int main() {
24     // throw exception
25     try {
26        cout << "
main invokes function throwException
";
27        throwException();
28        cout << "This should not print
";
29      }
30       catch (const exception&) { // handle exception
31          cout << "

Exception handled in main
";
32      }
33
34      cout << "Program control continues after catch in main
";
35   }

main invokes function throwException
  Function throwException throws an exception
  Exception handled in function throwException
  Function throwException rethrows exception

Exception handled in main
Program control continues after catch in main
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset