Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How do I know if an exception is being processed?

Reply
Thread Tools

How do I know if an exception is being processed?

 
 
Alan Johnson
Guest
Posts: n/a
 
      01-28-2007
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
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      01-28-2007
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.
 
Reply With Quote
 
 
 
 
Jerry Coffin
Guest
Posts: n/a
 
      01-28-2007
In article <(E-Mail Removed)>,
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Exception of type 'System.Web.HttpUnhandledException' wasthrown.Exception has been thrown by the target of an invocation.System.WebSystem.Exception jobs ASP .Net 1 11-16-2007 05:57 PM
while executing my client program i get the exception javax.naming.LinkException: [Root exception is javax.naming.LinkException: [Root exception is javax.naming.NameNotFoundException: remaining if plz anybody know how to solve this problem then mahesh Java 0 03-08-2007 12:26 PM
Form still being submitted despite being invalid =?Utf-8?B?TWFyayBQYXJ0ZXI=?= ASP .Net 4 07-25-2005 02:46 PM
I know, I know, I don't know Andries Perl Misc 3 04-23-2004 02:17 AM



Advertisments