What happens when i throw an exception in c++ destructor? [duplicate]
Possible Duplicate:
throwing exceptions out of a destr开发者_Python百科uctor
In C++ we should never throw an exception in the destructor . Does this code works as intended ?
struct a
{
~a( ) { }
};
struct b : public a
{
~b( )
{
throw 1;
};
};
bool c( )
{
a* d=new b;
try
{
delete d;
}
catch( int e )
{
return e;
}
return false;
}
Does this code works as intended ?
Did you try running it yourself? Also have a look at this FAQ - according to that, yes, it will work in your simple case, but in general, you shouldn't do it. Again, it depends on how you define "work as intended" - the program will run without errors but you will possibly leak memory because the object wasn't freed.
精彩评论