uncaught_exception() — Determine if an exception is being processed

Standards

Standards / Extensions C or C++ Dependencies

ANSI/ISO
C++

C++ only z/OS V1R2

Format

#include <exception>

bool uncaught_exception(void);

General description

The uncaught_exception() function returns true only if a thrown exception is currently being processed.

When uncaught_exception() is true, throwing an exception can result in a call of terminate().

Returned value

uncaught_exception() returns true after completing evaluation of a throw expression and before completing initialization of the exception declaration in the matching handler or calling unexpected() as a result of the throw expression.

Otherwise, uncaught_exception() returns false.

Example

#include <exception>
#include <iostream.h>

using namespace std;

class X
{
  public:
       ~X ();
};

X::~X()
{
  if (uncaught_exception ())
     printf (" X::~X called during stack unwind\n");
  else
     printf (" X::~X called normally\n");
}

int main()
{
   X x1;
   try
   {
      X x2;
      throw 1;
   }
   catch (...)  { /*...*/ }
   return 0;
}
// under a Standard-conforming implementation, this program yields
//  X::~X called during stack unwind
//  X::~X called normally

Related information