I am trying to call terminate function. [I think this function gets called when another exception arises during stack unwinding]. The same scenario I have written and trying to verify.
I am able to see a call being made to terminate() function but i am not sure why i am geting the Debug Error.
While trying to execute the following code in Visual studio 2008, I am getting an error message dialog box "Debug Error". The output is also being displayed:
Output:
In try block
In constructor of A
In constructor of B
In destructor of B
In destructor of A
Call to my_terminate
Why this "Debug Error" window appears while executing this code? It is expected behavior? How to remove this error?
class E
{
public:
const char* message;
E(const char* arg) : message(arg) { }
};
void my_terminate()
{
cout << "Call to my_terminate" << endl;
};
class A
{
public:
A() { cout << "In constructor of A" << endl; }
~A()
{
cout << "In destructor of A" << endl;
throw E("Exception thrown in ~A()");
}
};
class B
{
public:
B() { cout << "In constructor of B" << endl; }
~B() { cout << "In destructor of B" << endl; }
};
void main()
{
set_terminate(my_terminate);
try
{
cout << "In try block" << endl;
A a;
B b;
throw("Exception thrown in try block of main()");
}
catch (const char* e)
{
cout << "Exception: " << e << endl;
}
catch (...)
{
cout << "Some exception caught in main()" << endl;
}
cout << "Resume execution of main()" << endl;
getch();
}