Issue of the Implementation # S0672


std::uncaught_exception() returns wrong value after entering terminate() in some cases

Detailed Description

The description of uncaught_exception() throw() function states (ISO/IEC 14882, section 18.6.4):

Returns: true after completing evaluation of a throw-expression until either completing initialization of the exception-declaration in the matching handler or entering unexpected() due to the throw; or after entering terminate() for any reason other than an explicit call to terminate().

Apart from an explicit call to terminate() from the user's code, terminate() is called in the following situations (ISO/IEC 14882, section 15.5.1):

  1. when the exception handling mechanism, after completing evaluation of the expression to be thrown but before the exception is caught, calls a user function that exits via an uncaught exception.
  2. when the exception handling mechanism cannot find a handler for a thrown exception, or
  3. when the destruction of an object during stack unwinding exits using an exception, or
  4. when construction or destruction of a non-local object with static storage duration exits using an exception, or
  5. when execution of a function registered with atexit exits using an exception, or
  6. when a throw-expression with no operand attempts to rethrow an exception and no exception is being handled, or
  7. when unexpected throws an exception which is not allowed by the previously violated exception specification, and std::bad_exception is not included in that exception-specification.

However std::uncaught_exception called within a terminate_handler returns false in the cases 2,4,5,7 and true in the other cases (terminate_handler is specified using std::set_terminate()).

The attached examples demonstrate the above 7 situations when terminate() is called "implicitly" and display the return value of std::uncaught_exception().

Problem location(s) in the standard

Linux Standard Base C++ Specification 3.2, Chapter 9. Libraries, 9.1. Interfaces for libstdcxx that refers ISO/IEC 14882: 2003 Programming languages --C++, section 18.6.4




GCC Bugzilla 37477