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


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;
 7 #include 
 8 using std::exception;
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
26 cout << "This also should not print
27 } // end function throwException
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
43 cout << "Program control continues after catch in main
44 return 0;
45 } // 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

Introduction to Computers, the Internet and World Wide Web

Introduction to C++ Programming

Introduction to Classes and Objects

Control Statements: Part 1

Control Statements: Part 2

Functions and an Introduction to Recursion

Arrays and Vectors

Pointers and Pointer-Based Strings

Classes: A Deeper Look, Part 1

Classes: A Deeper Look, Part 2

Operator Overloading; String and Array Objects

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism


Stream Input/Output

Exception Handling

File Processing

Class string and String Stream Processing

Web Programming

Searching and Sorting

Data Structures

Bits, Characters, C-Strings and structs

Standard Template Library (STL)

Other Topics

Appendix A. Operator Precedence and Associativity Chart

Appendix B. ASCII Character Set

Appendix C. Fundamental Types

Appendix D. Number Systems

Appendix E. C Legacy Code Topics

Appendix F. Preprocessor

Appendix G. ATM Case Study Code

Appendix H. UML 2: Additional Diagram Types

Appendix I. C++ Internet and Web Resources

Appendix J. Introduction to XHTML

Appendix K. XHTML Special Characters

Appendix L. Using the Visual Studio .NET Debugger

Appendix M. Using the GNU C++ Debugger


show all menu

C++ How to Program
C++ How to Program (5th Edition)
ISBN: 0131857576
EAN: 2147483647
Year: 2004
Pages: 627
Similar book on Amazon © 2008-2017.
If you may any questions please contact us: