关于Stack unwinding
来源:互联网 发布:win10优化显卡 编辑:程序博客网 时间:2024/05/07 03:16
Stack unwinding (C++ only)
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. The automatic objects are destroyed in reverse order of their construction. (Automatic objects are local objects that have been declared auto or register, or not declared static or extern. An automatic object x is deleted whenever the program exits the block in which x is declared.)
If an exception is thrown during construction of an object consisting of subobjects or array elements, destructors are only called for those subobjects or array elements successfully constructed before the exception was thrown. A destructor for a local static object will only be called if the object was successfully constructed.
If during stack unwinding a destructor throws an exception and that exception is not handled, the terminate() function is called. The following example demonstrates this:
#include <iostream>
using namespace std;
struct E {
const char* message;
E(const char* arg) : message(arg) { }
};
void my_terminate() {
cout << "Call to my_terminate" << endl;
};
struct A {
A() { cout << "In constructor of A" << endl; }
~A() {
cout << "In destructor of A" << endl;
throw E("Exception thrown in ~A()");
}
};
struct B {
B() { cout << "In constructor of B" << endl; }
~B() { cout << "In destructor of B" << endl; }
};
int main() {
set_terminate(my_terminate);
try {
cout << "In try block" << endl;
A a;
B b;
throw("Exception thrown in try block of main()");
}
catch (const char* e) {
cout << "Exception: " << e << endl;
}
catch (...) {
cout << "Some exception caught in main()" << endl;
}
cout << "Resume execution of main()" << endl;
}
The following is the output of the above example:
In try block
In constructor of A
In constructor of B
In destructor of B
In destructor of A
Call to my_terminate
In the try block, two automatic objects are created: a and b. The try block
throws an exception of type const char*. The handler catch (const char* e)
catches this exception. The C++ run time unwinds the stack, calling the
destructors for a and b in reverse order of their construction. The
destructor for a throws an exception. Since there is no handler in the
program that can handle this exception, the C++ run time calls terminate().
(The function terminate() calls the function specified as the argument
to set_terminate(). In this example, terminate() has
been specified to call my_terminate().)
- 关于Stack unwinding
- stack unwinding
- Stack unwinding
- stack unwinding
- c++ stack unwinding
- STACK UNWINDING - 2013
- ARM Stack Unwinding
- 栈开解(stack unwinding)
- 关于析构函数在stack-unwinding中扔出异常的代码分析
- C++异常(2) - 栈展开(stack unwinding)
- exploit - win32 - stack winding and unwinding
- C++异常(2) - 栈展开(stack unwinding)
- 抛出异常与栈展开(stack unwinding)
- 抛出异常与栈展开(stack unwinding)
- 抛出异常与栈展开(stack unwinding)
- 抛出异常与栈展开(stack unwinding)
- 栈展开(stack unwinding)&在destructors中的exceptions
- 栈解旋unwinding
- 网站盈利模式分析分类
- 发现一个还行的 免费网络硬盘 Fs2You
- 介绍一款实用的东东——抗菌键盘保护膜
- 我的vim常用命令和script
- 问题: 什么是ASSERT()? ASSERT()和assert()的区别是什么?
- 关于Stack unwinding
- 转:运用J2EE创建灵活易扩展的企业应用程序探讨
- 一篇关于创业的文章
- 初尝UI编程的总结
- 点击简历五大关键词
- 假日调整方案公布(最新)
- IIS服务器不能用了, Exchange服务也不能用了
- 个人简历易忽略的细节
- 看完了服务器控件的开发了