Rethrowing an Exception
It is possible that an exception handler, upon receiving an exception, might decide either that it cannot process that exception or that it can process the exception only partially. In such cases, the exception handler can defer the exception handling (or perhaps a portion of it) to another exception handler. In either case, the handler achieves this by rethrowing the exception via the statement
throw;
Regardless of whether a handler can process (even partially) 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 16.6
Executing an empty tHRow statement that is situated outside a catch handler causes a call to function terminate, which abandons exception processing and terminates the program immediately. |
The program of Fig. 16.3 demonstrates rethrowing an exception. In main's try block (lines 3237), line 35 calls function throwException (lines 1127). The tHRowException function also contains a TRy block (lines 1418), from which the tHRow statement at line 17 throws an instance of standard-library-class exception. Function tHRowException's catch handler (lines 1924) catches this exception, prints an error message (lines 2122) and rethrows the exception (line 23). This terminates function throwException and returns control to line 35 in the try... catch block in main. The try block terminates (so line 36 does not execute), and the catch handler in main (lines 3841) catches this exception and prints an error message (line 40). [Note: 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 19 and 38).]
Figure 16.3. Rethrowing an exception.
(This item is displayed on pages 820 - 821 in the print version)
1 // Fig. 16.3: Fig16_03.cpp 2 // Demonstrating exception rethrowing. 3 #include 4 using std::cout; 5 using std::endl; 6 7 #include 8 using std::exception; 9 10 // throw, catch and rethrow exception 11 void throwException() 12 { 13 // throw exception and catch it immediately 14 try 15 { 16 cout << " Function throwException throws an exception "; 17 throw exception(); // generate exception 18 } // end try 19 catch ( exception & ) // handle exception 20 { 21 cout << " Exception handled in function throwException" 22 << " Function throwException rethrows exception"; 23 throw; // rethrow exception for further processing 24 } // end catch 25 26 cout << "This also should not print "; 27 } // end function throwException 28 29 int main() 30 { 31 // throw exception 32 try 33 { 34 cout << " main invokes function throwException "; 35 throwException(); 36 cout << "This should not print "; 37 } // end try 38 catch ( exception & ) // handle exception 39 { 40 cout << " Exception handled in main "; 41 } // end catch 42 43 cout << "Program control continues after catch in main "; 44 return 0; 45 } // end main
|