xmllmx
xmllmx

Reputation: 42379

Is it possible to have two or more active exceptions at the same time?

C++17 introduces a new function std::uncaught_exceptions:

Detects how many exceptions have been thrown or rethrown and not yet entered their matching catch clauses.

The following code:

#include <iostream>

using namespace std;

struct A
{
    ~A()
    {
        cout << std::uncaught_exceptions() << endl;
    }
};

int main()
{
    try
    {
        try
        {

            A a1;
            throw 1;
        }
        catch (...)
        {
            A a2;
            throw;
        }
    }
    catch (...)
    {
        A a3;
    }   
}

outputs:

1

1

0

Is it possible to have two or more active exceptions at the same time?

Is there any example?

Upvotes: 13

Views: 1076

Answers (1)

Nicol Bolas
Nicol Bolas

Reputation: 474016

Yes. Throw an exception from a destructor that was called due to stack unwinding from handling another exception:

struct D
{
  ~D()
  {
    std::cout << std::uncaught_exceptions() << std::endl;
  }
};

struct E
{
  ~E()
  {
    try
    {
      D d_;
      throw 2;
    }
    catch(...)
    {
      std::cout << std::uncaught_exceptions() << std::endl;
    }
  }
};

int main()
{
  try
  {
    D d;
    E e;
    throw 1;
  }
  catch(...)
  {
  }
}

d's destructor will be called while 1 is still an active exception. So std::uncaught_exceptions() inside ~d will be 1.

For e, its destructor will be called while 1 is an active exception. Its destructor will be called. It will construct a D, then throw again. But since neither 1 nor 2 have been caught at this point, std::uncaught_exceptions() inside of ~d_ will be 2.

The whole purpose of std::uncaught_exceptions is to detect this very condition. It allows you to know if an object is being destroyed due to stack unwinding or through normal execution. This lets you know if you should do different kinds of cleanup. Consider a RAII object that will rollback a transaction if it is destroyed due to stack unwinding.

Upvotes: 13

Related Questions