By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,485 Members | 1,031 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,485 IT Pros & Developers. It's quick & easy.

throw in destructors

P: n/a
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?

Jan 21 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
* Shark:
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?


It's a requirement for standard collection items, and otherwise it's a
guideline.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jan 21 '06 #2

P: n/a
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
Jan 21 '06 #3

P: n/a
Shark 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?


See
http://www.parashift.com/c++-faq-lit....html#faq-17.3

HTH,
- J.
Jan 21 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.