Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Throwing an exception within a destructor.. 2

Status
Not open for further replies.

bodhi

Programmer
Jan 3, 2002
67
GB
Was reading the other night about SEH in 'official' C++ and it mentioned in the article that you should *never* throw an exception within a destructor.

The explanation was vague at best and I was hoping that someone had come across this or could explain the dangers/problem...

I'd never heard of this before and am curious to know why it's such a big no-no..
 
One of the main issues is that the destructor could be called within the context of the stack unwinding from a previous exception being thrown. This condition can cause the application to terminate immediately.

-pete
 
Thanks for the reply.

So effectively you would then have two valid exceptions - one from the initial error (from which the destructor gets called) and one from error x occuring within the destructor ?

Is the rule then that two valid exceptions results in an automatic terminate ?
 
I believe so, because how do you unwind the stack unwinding? Wouldn't that be a rewind? Oh dear... now i'm dizzy [lol]

-pete
 
1)
The concept of writing exception safe/neutral code is based on thatyou can rely on the destructor managing its task, that is, with a situation is infact thrown just put all your trust in destructors.

2)
Another way of thinking about it is "destruction must always succeed". Conceptually, if an exception is thrown from a destructor that would mean that the destruction did infact fail.

If we don't code by "destruction must always succeed" we can get into quite hairy situations, like where you must delete stuff when quitting, but since the deletion fails you can't quit.

Exceptions are (preferably) used in such a way that for example a user can make corrective actions and perhaps try again. But with a failed destructor, what on earth is a poor user supposed to do (perhaps something like setting a member's value so it can be immediately destroyed - wouldn't that be really silly?)...

>Is the rule then that two valid exceptions results in an automatic terminate ?

Its a rule that 2 simultaneous exceptions will make your application die quite ungracefully.

/Per

if (typos) cout << &quot;My fingers are faster than my brain. Sorry for the typos.&quot;;
 
The destructor is commonly used to “clean up” when an object is no longer necessary.

Destrctor deallocates the memory that was previously reserved for class elements.
 
dlldelhi, strictly speaking, a destructor don't deallocate any memory. Imagine auto (on stack) or static (or global) class var with class members. We never write any member destructor calls or any deallocators call in the outer class destructor, don't you?. Destruction (of object) and deallocation (of memory chunk) are separate things in C++.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top