On 21 Jan 2006 00:45:00 -0800, "Shark" <cp*******@yahoo.com> wrote:
I read that in the C++ standard library destructors are not supposed to
throw exceptions. Is this true of only the library, or is it true of
C++ classes in general?
STL destructors do not throw because that is how they are made. But
the C++ language doesn't prohibit throwing an exception from a
destructor.
Although it is possible to throw something from a destructor if you
are writing the code, it is considered dangerous and very bad practice
to do so. If you have to call a function within the body of a
destructor which might throw, you should use a try/catch block and
handler to ensure that the exception won't leave your destructor.
The reason that it is dangerous is because the destructor might have
been called as the result of stack unwinding from another exception.
Since the original exception is still unhandled, any new exception
would cause terminate() to be called immediately, and no further stack
cleanup is executed (i.e. no more destructors are called).
--
Bob Hairgrove
No**********@Home.com