How do I know if an exception is being processed?

Discussion in 'C++' started by Alan Johnson, Jan 28, 2007.

  1. Alan Johnson

    Alan Johnson Guest

    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
     
    Alan Johnson, Jan 28, 2007
    #1
    1. Advertising

  2. Alan Johnson

    Ian Collins Guest

    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.
     
    Ian Collins, Jan 28, 2007
    #2
    1. Advertising

  3. Alan Johnson

    Jerry Coffin Guest

    In article <>,
    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.
     
    Jerry Coffin, Jan 28, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Leon
    Replies:
    1
    Views:
    340
  2. smccreadie
    Replies:
    0
    Views:
    228
    smccreadie
    Oct 27, 2006
  3. Lars
    Replies:
    2
    Views:
    164
    Aaron Bertrand - MVP
    Oct 29, 2003
  4. mrwoopey
    Replies:
    0
    Views:
    213
    mrwoopey
    Oct 30, 2003
  5. Andries

    I know, I know, I don't know

    Andries, Apr 23, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    240
    Gregory Toomey
    Apr 23, 2004
Loading...

Share This Page