-2

Possible Duplicate:
throwing exceptions out of a destructor

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; 
}
Community
  • 1
  • 1
brett
  • 5,379
  • 12
  • 43
  • 48

1 Answers1

0

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.

casablanca
  • 69,683
  • 7
  • 133
  • 150