int vali...">

Failure after exception

Why is this crash after std :: bad_exception detected? (I am using 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; } 
+8
c ++ exception
source share
1 answer

Because you cannot stop exceptions from the constructor initialization list. After you catch it, it will automatically close. (Then it crashes because you have an exception without participating.)

This is good: if your members cannot be initialized correctly, your class cannot exist normally.

+12
source share

All Articles