捕获std :: bad_exception后为什么会崩溃?(我正在使用VC7)
#include "stdafx.h"
#include <exception>
int validateInt (int x) throw (int,std::bad_exception) {
if ( 0 == x ) {
throw std::bad_exception("x");
}
return x;
}
class C {
int i;
public:
C(int);
};
C::C(int ii)
try : i( validateInt(ii) ) {
std::cout << "I'm in constructor function body\n";
} catch (std::exception& e) {
std::cout << "I caught an exception...\n";
}
int _tmain(int argc, _TCHAR* argv[]) {
C a(0);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
GMa*_*ckG 12
因为您无法阻止异常离开构造函数初始化列表.抓住它之后,它会自动重新生成.(然后崩溃,因为你有一个无法处理的异常.)
这是一件好事:如果您的成员无法正确初始化,您的课程就无法正常存在.