std::uncaught_exception, std::uncaught_exceptions

From cppreference.com
< cpp‎ | error
 
 
Utilities library
Type support (basic types, RTTI, type traits)
Dynamic memory management
Error handling
Program utilities
Variadic functions
Date and time
Function objects
(C++11)
Relational operators
Optional and any
(C++17)
(C++17)
Pairs and tuples
(C++11)
(C++17)
Swap, forward and move
(C++14)
(C++11)
(C++11)
Type operations
(C++11)
(C++17)
 
Error handling
Exception handling
uncaught_exceptionuncaught_exceptions
(C++17)
(C++11)
(C++17)
Exception handling failures
(C++11)
(deprecated)
(deprecated)
(C++11)(deprecated)
(deprecated)
 
Defined in header <exception>
bool uncaught_exception();
(1) (deprecated in C++17)
int uncaught_exceptions();
(2) (since C++17)
1) Detects if the current thread has a live exception object, that is, an exception has been thrown or rethrown and not yet entered a matching catch clause, std::terminate or std::unexpected. In other words, std::uncaught_exception detects if stack unwinding is currently in progress.
2) Detects how many exceptions have been thrown or rethrown and not yet entered their matching catch clauses.

Sometimes it's safe to throw an exception even while std::uncaught_exception() == true. For example, if stack unwinding causes a stack-allocated object to be destructed, the destructor for that object could run code that throws an exception as long as the exception is caught by some catch block before escaping the destructor.

Contents

[edit] Parameters

(none)

[edit] Return value

1) true if stack unwinding is currently in progress in this thread.
2) The number of exception objects currently uncaught.

[edit] Exceptions

(none) (until C++11)
noexcept specification:  
noexcept
  
(since C++11)

[edit] Notes

An example where int-returning uncaught_exceptions is used is the boost.log] library: the expression BOOST_LOG(logger) << foo(); first creates a guard object and records the number of uncaught exceptions in its constructor. The output is performed by the guard object's destructor unless foo() throws (in which case the number of uncaught exceptions in the destructor is greater than what the constructor observed)

[edit] Example

#include <iostream>
#include <exception>
#include <stdexcept>
 
struct Foo {
    ~Foo() {
        if (std::uncaught_exception()) {
            std::cout << "~Foo() called during stack unwinding\n";
        } else {
            std::cout << "~Foo() called normally\n";
        }
    }
};
int main()
{
    Foo f;
    try {
        Foo f;
        std::cout << "Exception thrown\n";
        throw std::runtime_error("test exception");
    } catch (const std::exception& e) {
        std::cout << "Exception caught: " << e.what() << '\n';
    }
}

Output:

Exception thrown
~Foo() called during stack unwinding
Exception caught: test exception
~Foo() called normally

[edit] See also

function called when exception handling fails
(function)
shared pointer type for handling exception objects
(typedef)

[edit] External links