0. stack unwinding. when an exception is thrown and control passes from a try block to a handler, the C ++ run time CILS Destructors for all automatic objects constructed since the beginning of the try block. this process is calledStack 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;
Ee;
Bb * PBB = new dd ();
Delete PBB;
}
Catch (...)
{
Outputdebugstring (_ T ("--- get exception caught. \ n "));
}
Return 0;
}
Output:
View code 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.1. exception cost: when to throw and when not
To minimize your exception-related costs, compile without support for exceptions when that is feasible; Limit your use of try blocks and exception specifications to those locations where you honestly need them; and throw exceptions only under conditions that are truly exceptional.
1) Level 1: compiling with exception support is the first factor to affect performance. if you compile your program without exception support, you can gain runtime performance improved. here, exception support basically means: At runtime, there needs extra space/time to keep record of fully constructed objects so that when exception thrown, stack unwinding, objects can be destructed correctly.
2) Level 2: A second cost of exception-handling arises from try blocks, and you pay it whenever you use one, I. E ., whenever you decide you want to be able to catch exceptions. different compilers implement try blocks in different ways, so the cost varies from compiler to compiler. as a rough estimate, please CT your overall code size to increase by 5-10% and your runtime to go up by a similar amount if you use try blocks. this assumes no exceptions are thrown; what we're re discussing here is just the cost of having try blocks in your programs. to minimize this cost, you shoshould avoid unnecessary try blocks.
3) Level 3: The cost of throwing an exception. Compared to a normal function return, returning from a function by throwing an exception may be as much as three orders of magn1_slower.