What happens when we throw
from a destructor? I know that it causes terminate()
to be called, and memory is indeed freed and the destructor is called, but, is this before or after throw
is called from foo
? Perhaps the issue here is that throw
is used while the stack is unwinding that is the problem.
Is this before or after throw is called from foo?
This is what is happening:
foo()
is calleda
of type A
is created on the stacka
is called, which throws another exceptionstd::terminate
is called -- which is nothing but abandoning the exception handling mechanism:From C++0x draft:
15.5.1 The std::terminate() function
1 In the following situations exception handling must be abandoned for less subtle error handling techniques:
[...] — when the destruction of an object during stack unwinding (15.2) exits using an exception, or
2 In such cases, std::terminate() is called (18.7.3). In the situation where no matching handler is found, it is implementation-defined whether or not the stack is unwound before std::terminate() is called. In all other situations, the stack shall not be unwound before std::terminate() is called. An implementation is not permitted to finish stack unwinding prematurely based on a determination that the unwind process will eventually cause a call to std::terminate().
Note: Emphasis mine