惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

F
Fortinet All Blogs
aimingoo的专栏
aimingoo的专栏
V
Visual Studio Blog
罗磊的独立博客
爱范儿
爱范儿
J
Java Code Geeks
博客园 - 司徒正美
N
Netflix TechBlog - Medium
Microsoft Security Blog
Microsoft Security Blog
美团技术团队
小众软件
小众软件
Google DeepMind News
Google DeepMind News
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
V2EX
博客园 - 聂微东
云风的 BLOG
云风的 BLOG
WordPress大学
WordPress大学
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Jina AI
Jina AI
Y
Y Combinator Blog
博客园 - 叶小钗
人人都是产品经理
人人都是产品经理
Martin Fowler
Martin Fowler
Vercel News
Vercel News

博客园 - 能巴

[SQL Server]Index/deadlock 业务学习 [LoadRunner]负载测试工具 一般函数指针和类的成员函数指针 [P4 password]Avoiding the Perforce Prompt for Password in Windows [收藏转载]2011 APP年终总结——日均160元的收入经历 [收藏转载]我所积累的20条编程经验 [收藏转载]明星软件工程师的十种特质 Importance of Side Projects [转载]最好的HTML 5编码教程和参考手册分享 . [Mobility]移动开发概览 How Browser Works [c++ 11x rvalue reference] [转载]The Biggest Changes in C++11 (and Why You Should Care) Exception Cost STL算法find_if和find QT信号和槽 How to set NoStepInto for VS debugging Review: Function Pointer
Summary of Exception
能巴 · 2011-07-28 · via 博客园 - 能巴

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:

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