Software & Finance

C++ - Exceptions and Stack Unwinding Process

Exceptions (try, throw, catch) are very useful in C++. With in the catch block, we can either write the log message or let the user knows about what went wrong. Sometimes it can be used to come out of the nested for loops for 3 to 4 levels.

When throwing an exception with in a try block, program control then directly goes to the corresponding catch block or default catch(...) by doing the stack unwinding.

Stack unwinding is the very good feature in C++. During this process all the destructors are called in the reverss order.

Source Code

#include<iostream>

class CConfig

{

std::string m_name;

public:

CConfig()

{

m_name = "";

}

CConfig(constchar *name)

{

this->m_name = name;

}

CConfig(const CConfig &other)

{

this->m_name = other.m_name;

}

const CConfig& operator = (const CConfig &other)

{

this->m_name = other.m_name;

return *this;

}

void CalculateError()

{

std::cout << "Exception Occurs\n";

throw -1; // just throw an exception

}

};

class MyObjectONE

{

public:

MyObjectONE() { }

~MyObjectONE()

{

std::cout << "Calling destructor of MyObejectOne\n";

}

};

class MyObjectTWO

{

public:

MyObjectTWO() { }

~MyObjectTWO()

{

std::cout << "Calling destructor of MyObjectTWO\n";

}

};

class MyObjectTHREE

{

public:

MyObjectTHREE() { }

~MyObjectTHREE()

{

std::cout << "Calling destructor of MyObjectTHREE\n";

}

};

void TestException(CConfig &config)

{

config.CalculateError(); // Throws an exception

}

void TestFunc()

{

std::cout << "TestFunc Begins\n";

CConfig theConfig("MyApp");

MyObjectONE one;

MyObjectTWO two;

MyObjectTHREE three;

TestException(theConfig);

// The remaining lines will not get executed because of throwing exception in TextException