top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How can I handle a destructor that fails?

+1 vote
284 views

explain with example in c++..

posted Dec 10, 2014 by Anwar

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+2 votes
 
Best answer

Write a message to a log-_le. But do not throw an exception.

The C++ rule is that you must never throw an exception from a destructor that is being called during the "stack unwinding" process of another exception.

For example

If someone says throw Foo(), the stack will be unwound so all the stack frames between the throw Foo() and the } catch (Foo e) { will get popped. This is called stack unwinding. During stack unwinding, all the local objects in all those stack frames are destructed. If one of those destructors throws an exception (say it throws a Bar object), the C++ runtime system is in a no-win situation: should it ignore the Bar and end up in the } catch (Foo e) { where it was originally headed? Should it ignore the Foo and look for a } catch (Bare) { handler? There is no good answer:either choice loses information. So the C++ language guarantees that it will call terminate() at this point, and terminate() kills the process. Bang you're dead.

answer Dec 13, 2014 by Mohammed Hussain
...