471,321 Members | 1,739 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,321 software developers and data experts.

throw in destructors

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
3 1620
* 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
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
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.

Similar topics

3 posts views Thread by Rajesh Garg | last post: by
4 posts views Thread by Jurko Gospodnetić | last post: by
26 posts views Thread by Michi Henning | last post: by
8 posts views Thread by Edward Diener | last post: by
17 posts views Thread by Phlip | last post: by
5 posts views Thread by Kenneth Porter | last post: by
8 posts views Thread by Grizlyk | last post: by
5 posts views Thread by siddhu | last post: by
6 posts views Thread by Jeff Newman | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.