Can I use std::current_exception during stack unwinding?

C++ Standard defines current_exception() in section § 18.8.5 [propagation] :

(emphasis mine)

exception_ptr current_exception() noexcept;

Returns: An exception_ptr object that refers to the currently handled
exception (15.3) or a copy of the currently handled exception, or a
null exception_ptr object if no exception is being handled. The
referenced object shall remain valid at least as long as there is an
exception_ptr object that refers to it.

And § 15.3 [except.handle], notes 7 and 8 :

  1. A handler is considered active when initialization is complete for the
    parameter (if any) of the catch clause. [ Note: The stack will have
    been unwound at that point
    . — end note ]

  2. The exception with the most recently activated handler that is still
    active
    is called the currently handled exception.

The exception returned by current_exception() is defined as the “currently handled exception”, which is the exception of the most recent active handler, and a handler is active only when stack unwinding completed.


As your tests have shown, there is no “active handler” during stack unwinding, so there is no “currently handled exception” either : in that case, current_exception() will return a null exception_ptr.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)