我试图理解为什么从析构函数中抛出异常会导致程序崩溃。我发现很多例子都是两个对象同时抛出多个异常,编译器无法处理多个异常,但在我的情况下,只有一个异常从析构函数中抛出。为什么我的程序仍然崩溃?
class MyClass {
private:
string name;
public:
MyClass (string s) :name(s) {
cout << "constructor " << name << endl;
}
~MyClass() {
cout << "Destroying " << name << endl;
throw "invalid";
}
};
int main( )
{
try {
MyClass mainObj("M");
}
catch (const char *e) {
cout << "exception: " << e << endl;
cout << "Mission impossible!\n";
}
catch (...) {
cout << "exception: " << endl;
}
return 0;
}