Stack Unwinding in C++

Estudies4you
Stack Unwinding

 STACK UNWINDING 

18. Discuss about stack unwinding. 
Answer:
Stack Unwinding
whenever exception is thrown, usually the control of program shifts from the try block and looks for a corresponding handler. During this session the C++ run time calls destructor relative to all automatic objects created while the initiation of try block. This consequence is usually referred as stack unwinding. As a result of it, all the automatic objects which were created, get destroyed in the reverse order of creation.
Assume that the program control is busy in creating an object and this object in turn processes sub-objects or array elements. Now. consider that an exception is raised while this process is in progress. To manage such consequences, destructors are called for those sub-objects array elements which were executed successfully prior to the raising of any exception. 
The terminate function is called on the account that the destructor throws- an exception and there is none to handle this exception, while the stack unwinding process is in progress. 

Program 
The following program demonstrates stack unwinding

#include<iostream>

using namespace std;

void func_a() throw (int) 

{

cout<<"\n func_a() Start ";

throw 50;

cout<<"\nfunc_a() End "; 

}

void func_b() throw(int)

{

cout<<"\n func_b() Start";

func_a();

cout<<"\n func_b() End";

}

void func_c() throw(int)

{

cout<<"\n func_c Start";

try

{

func_b();

}

catch(int i)

{

cout<<"\n Caught Exception:"<<i;

}

cout<<\n func_c() End";

}

int main()

{

func_c();

getchar();

return 0; 

}

Output 

Examples of Stack Unwinding in c++,programs for Stack Unwinding in c++,c++ notes,c++ notes unitwise,oops using c++ notes jntuh


To Top