有些文章总结"永远不会从析构函数中抛出异常",而"std :: uncaught_exception()没有用",例如:
但似乎我没有明白这一点.所以我写了一个小测试示例(见下文).
由于测试示例一切正常,我非常感谢有关它可能出错的一些评论?
测试结果:
./主要
Foo::~Foo(): caught exception - but have pending exception - ignoring
int main(int, char**): caught exception: from int Foo::bar(int)
./main 1
Foo::~Foo(): caught exception - but *no* exception is pending - rethrowing
int main(int, char**): caught exception: from Foo::~Foo()
例:
// file main.cpp
// build with e.g. "make main"
// tested successfully on Ubuntu-Karmic with g++ v4.4.1
#include <iostream>
class Foo {
public:
int bar(int i) {
if (0 == i) …Run Code Online (Sandbox Code Playgroud) 从相关的c ++标准部分:
引用构造函数的函数try-block的处理程序中的对象的任何非静态成员或基类,或该对象的析构函数导致未定义的行为.
例如.
T::~T()
{
try {
this->nonstatic_member; // iff I read the quote correctly
} catch( ... ) {
}
}
Run Code Online (Sandbox Code Playgroud)
那为什么这个未定义的行为?