【问题标题】:exception handling, unexpected terminator in c++异常处理,c ++中的意外终止符
【发布时间】:2013-04-08 21:50:46
【问题描述】:
以下代码是关于异常处理的。我得到了输出:
Botch::f()
I'll be back!
为什么没有抓到水果?谢谢!
忽略这个。我想我已经提供了足够的细节。
#include <exception>
#include <iostream>
using namespace std;
void terminator() {
cout << "I'll be back!" << endl;
exit(0);
}
void (*old_terminate)() = set_terminate(terminator);
class Fruit {};
class Botch {
public:
void f() throw(Fruit, bad_exception) {
cout << "Botch::f()" << endl;
throw Fruit();
}
~Botch() { throw 'c'; }
};
int main() {
try{
Botch b;
b.f();
} catch(Fruit&) {
cout << "inside catch(Fruit)" << endl;
} catch(bad_exception&) {
cout << "caught a bad_excpetionfrom f" << endl;
}
}
【问题讨论】:
标签:
c++
exception
terminate
【解决方案1】:
因为在您的 Fruit 异常的堆栈展开期间,您抛出了另一个异常(来自 Botch 析构函数)。所以你的终结者被调用了。这就是为什么从析构函数中抛出异常是个坏主意,
【解决方案2】:
Fruit 未被捕获,因为代码从未到达该 catch 子句。在main 的try 块中,对b.f() 的调用会引发Fruit 类型的异常。作为响应,代码在进入 catch 子句之前销毁 Botch 对象。 Botch 的析构函数抛出另一个异常,并触发对 terminate 的调用。
【解决方案3】:
当在 main 中调用 b.f() 时,会抛出 Fruit。然后执行离开try 块,在任何catch 处理程序可以捕捉到Fruit 之前,b 被销毁并抛出'c'。在Fruit 仍处于活动状态时引发第二个异常会导致终止,无论任何捕获处理程序如何。
这就是为什么你永远不会从析构函数中抛出的原因。
【解决方案4】:
因为程序流程如下:
try{
Botch b;
b.f();
//-> exception of class Fruit has been thrown
//-> Stack unwinding: during stack unwinding object b,
//which is on stack is destroyed, and its destructor is called
//-> this destructor ~Botch() { throw 'c'; } throws another exception
//and this caused call of your terminator()
} catch(Fruit&) { // so we are never here