0. 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.

Two notes: 1) local objects will be destructed in reverse order; 2) if another exception thrown during stack unwinding in destructing an object, as no one can handle that exception, Terminate will be called to end the program. 

View Code
class BB
{
public:    
    BB() {  OutputDebugString(_T(
"BB::BB()\n")); }
    
virtual ~BB() {  OutputDebugString(_T("BB::~BB()\n")); throw 0; }
};

class DD : public BB
{
public:
    DD() {  OutputDebugString(_T(
"DD::DD()\n")); }
    
~DD() {  OutputDebugString(_T("DD::~DD()\n")); }
};

class EE
{
public:
    EE() { OutputDebugString(_T(
"EE::EE\n")); }
    
~EE() { OutputDebugString(_T("EE::~EE\n"));}
};

class FF
{
public:
    FF() { OutputDebugString(_T(
"FF::FF\n")); }
    
~FF() { OutputDebugString(_T("FF::~FF\n")); /*throw 1;*/}
};

int _tmain(int argc, _TCHAR* argv[])
{    
    
try
    {
        FF ff;
        EE ee;
        BB
* pBB = new DD();
        delete pBB;
    }    
    
catch (...)
    {
        OutputDebugString(_T(
"---get exception caught.\n"));
    }

    
return 0;
}

Output:

FF::FF
EE::EE
BB::BB()
DD::DD()
DD::
~DD()
BB::
~BB()
First
-chance exception at 0x76d8b727 (KernelBase.dll) in effPractice.exe: Microsoft C++ exception: int at memory location 0x0045f784..
EE::
~EE
FF::
~FF
---get exception caught.

相关文章: