Summary of exception

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.