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

How do I know if an exception is being processed?

P: n/a
FAQ 17.3 says: "never throw an exception from a destructor while
processing another exception"

What mechanism is available for me to know if an exception is being
processed?

I imagine this knowledge being useful in a case like the following. You
have an operation that needs to be done in pairs (e.g. locking and
unlocking a Mutex). To make life easier, you create a class to do these
for you when you enter/leave a scope. The action to be performed when
leaving a scope, however, can throw an exception. So you may want to do
something like:

class Guard
{
private:
Mutex & m_mutex ;
public:
explicit Guard(Mutex & mutex) : m_mutex(mutex)
{
m_mutex.lock() ;
}

~Guard()
{
try
{
m_mutex.unlock() ;
}
catch (...)
{
if (/* Not processing another exception. */)
throw ;
/* Maybe do some logging or something here. */
}
}
} ;

--
Alan Johnson
Jan 28 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Alan Johnson wrote:
FAQ 17.3 says: "never throw an exception from a destructor while
processing another exception"

What mechanism is available for me to know if an exception is being
processed?
Save yourself a lot of trouble and don't throw exceptions from destructors.

--
Ian Collins.
Jan 28 '07 #2

P: n/a
In article <Du******************************@comcast.com>,
aw***@yahoo.com says...
FAQ 17.3 says: "never throw an exception from a destructor while
processing another exception"

What mechanism is available for me to know if an exception is being
processed?
That's what uncaught_exception() does -- sort of.
I imagine this knowledge being useful in a case like the following.
See:

http://www.gotw.ca/gotw/047.htm

You're characterized the utility of the information perfectly: it's
purely imaginary. Your code fits closely with how most people thought it
would be useful -- but it turns out it's a mistake, and I've yet to see
anybody come up with a way to use unaught_exception that was useful.
Likewise, there's really no way to let an exception escape for a dtor
without it potentially causing problems -- and the difference between
"potentially" and "inevitably" is essentially nonexistent.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jan 28 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.