Demonstrating stack unwinding : Exception « Exceptions « C++ Tutorial






#include <iostream>
using std::cout;
using std::endl;

#include <stdexcept>
using std::runtime_error;

void f3() throw ( runtime_error )
{
   cout << "In f 3" << endl;
   throw runtime_error( "runtime_error in f3" ); 
}

void f2() throw ( runtime_error )
{
   cout << "f3 is called inside f2" << endl;
   f3(); 
}

void f1() throw ( runtime_error )
{
   cout << "f2 is called inside f1" << endl;
   f2();
}

int main()
{
   try 
   {
      cout << "f1 is called inside main" << endl;
      f1();
   }
   catch ( runtime_error &error )
   {
      cout << "Exception occurred: " << error.what() << endl;
      cout << "Exception handled in main" << endl;
   }

   return 0;
}
f1 is called inside main
f2 is called inside f1
f3 is called inside f2
In f 3
Exception occurred: runtime_error in f3
Exception handled in main








6.3.Exception
6.3.1.Declaring exception specifications in class hierarchy
6.3.2.Handling Derived-Class Exceptions
6.3.3.Demonstrating stack unwinding