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 than 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.


Image Common Programming Error 17.5

Executing an empty throw statement outside a catch handler abandons exception processing and terminates the program immediately.


The program of Fig. 17.3 demonstrates rethrowing an exception. In main’s try block (lines 29–34), line 32 calls function throwException (lines 8–24). The throwException function also contains a try block (lines 11–15), from which the throw statement in line 14 throws an instance of standard-library-class exception. Function throwException’s catch handler (lines 16–21) catches this exception, prints an error message (lines 18–19) and rethrows the exception (line 20). This terminates function throwException and returns control to line 32 in the try...catch block in main. The try block terminates (so line 33 does not execute), and the catch handler in main (lines 35–38) catches this exception and prints an error message (line 37). Since we do not use the exception parameters in the catch handlers of this example, we omit the exception parameter names and specify only the type of exception to catch (lines 16 and 35).


 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   {
10      // throw exception and catch it immediately
11      try
12      {
13         cout << "  Function throwException throws an exception ";
14         throw exception(); // generate exception
15      } // end try
16      catch ( exception & ) // handle exception
17      {
18         cout << "  Exception handled in function throwException"
19            << "   Function throwException rethrows exception";
20         throw; // rethrow exception for further processing
21      } // end catch
22
23      cout << "This also should not print ";
24   } // end function throwException
25
26   int main()
27   {
28      // throw exception
29      try
30      {
31         cout << " main invokes function throwException ";
32         throwException();
33         cout << "This should not print ";
34      } // end try
35      catch ( exception & ) // handle exception
36      {
37         cout << " Exception handled in main ";
38      } // end catch
39
40      cout << "Program control continues after catch in main ";
41   } // end main


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


Fig. 17.3. Rethrowing an exception.

..................Content has been hidden....................

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