捕获异常后崩溃



为什么在捕获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 bodyn";
} catch (std::exception& e) {  
    std::cout << "I caught an exception...n";
}
int _tmain(int argc, _TCHAR* argv[]) {
    C a(0);
    return 0;
}

因为您无法阻止异常离开构造函数初始化列表。捕获它之后,它会自动重新抛出。(然后它崩溃了,因为你有一个未处理的异常。)

这是一件好事:如果你的成员不能正确初始化,你的类就不能正确存在。

最新更新