First page Back Continue Last page Graphics

fig13_02.cpp (1 of 2)

  • 1 // Fig. 13.2: fig13_02.cpp
  • 2 // Demonstrating exception rethrowing.
  • 3 #include <iostream>
  • 4
  • 5 using std::cout;
  • 6 using std::endl;
  • 7
  • 8 #include <exception>
  • 9
  • 10 using std::exception;
  • 11
  • 12 // throw, catch and rethrow exception
  • 13 void throwException()
  • 14 {
  • 15 // throw exception and catch it immediately
  • 16 try {
  • 17 cout << " Function throwException throws an exception\n";
  • 18 throw exception(); // generate exception
  • 19
  • 20 } // end try
  • 21
  • 22 // handle exception
  • 23 catch ( exception &caughtException ) {
  • 24 cout << " Exception handled in function throwException"
  • 25 << "\n Function throwException rethrows exception";
  • 26
  • 27 throw; // rethrow exception for further processing
  • 28
  • 29 } // end catch