Stack Unwinding | Stack Unwinding C++

Stack Unwinding | Stack Unwinding in C++

Stack Unwinding:

When an exception is thrown and control passes from a try block to a handler, the C++ run time calls destructors for all automatic objects constructed since the beginning of the try block. This process is called stack unwinding.

Stack Unwinding in C++

#include<iostream>
#include<stdexcept>
using namespace std;
void function3() throw(runtime_error)
{
cout<<"Function 3."<<endl;
throw runtime_error("Runtime error in Function 3.\n");
}
void function2() throw(runtime_error)
{
cout<<"Function 2."<<endl;
function3();
}
void function1() throw(runtime_error)
{
cout<<"Function 1."<<endl;
function2();
}
int  main()
{
try
{
 cout<<"Function 1 called."<<endl;
 function1();
}
catch(runtime_error r)
{
 cout<<r.what();
}
return 0;  }

OUTPUT:
Function 1 called.
Function 1.
Function 2.
Function 3.
Runtime error in Function 3.

You must be also searching for these programming languages : 


tags: stack unwinding, stack unwinding in C++, stack unwinding C++, Stack Unwinding example.

Post a Comment

Previous Post Next Post