Summary of Exception

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


Summary of ExceptionSummary of ExceptionView CodeclassBB

{

public:

BB() { OutputDebugString(_T(
"BB::BB()n")); }

virtual~BB() { OutputDebugString(_T("BB::~BB()n"));throw0; }

};



classDD :publicBB

{

public:

DD() { OutputDebugString(_T(
"DD::DD()n")); }

~DD() { OutputDebugString(_T("DD::~DD()n")); }

};



classEE

{

public:

EE() { OutputDebugString(_T(
"EE::EEn")); }

~EE() { OutputDebugString(_T("EE::~EEn"));}

};



classFF

{

public:

FF() { OutputDebugString(_T(
"FF::FFn")); }

~FF() { OutputDebugString(_T("FF::~FFn"));/throw 1;/}

};



int_tmain(intargc, _TCHARargv[])

{

try

{

FF ff;

EE ee;

BB
pBB=newDD();

delete pBB;

}

catch(...)

{

OutputDebugString(_T(
"---get exception caught.n"));

}



return0;

}

Output:
Summary of ExceptionSummary of ExceptionView CodeFF::FF

EE::EE

BB::BB()

DD::DD()

DD::
~DD()

BB::
~BB()

First
-chance exception at0x76d8b727(KernelBase.dll)ineffPractice.exe: Microsoft C++exception:intat memory location0x0045f784..

EE::
~EE

FF::
~FF

---getexception caught.

  1. Exception Cost: When to throw and when not to

    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, expect 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 discussing here is just the cost of having try blocks in your programs. To minimize this cost, you should 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 magnitude slower.

原文链接: https://www.cnblogs.com/taoxu0903/archive/2011/07/28/2119458.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/29469

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月8日 上午6:54
下一篇 2023年2月8日 上午6:55

相关推荐