Namespaces
Variants
Views
Actions

std::uncaught_exception

From cppreference.com
 
 
 
Error handling
Exception handling
exception
uncaught_exception
exception_ptr(C++11)
make_exception_ptr(C++11)
current_exception(C++11)
rethrow_exception(C++11)
nested_exception(C++11)
throw_with_nested(C++11)
rethrow_if_nested(C++11)
Exception handling failures
terminate
terminate_handler
get_terminate(C++11)
set_terminate
unexpected(deprecated)
bad_exception
unexpected_handler(deprecated)
get_unexpected(C++11)(deprecated)
set_unexpected(deprecated)
Exception categories
logic_error
invalid_argument
domain_error
length_error
out_of_range
runtime_error
range_error
overflow_error
underflow_error
Error codes
Error codes
errno
Assertions
assert
system_error facility
error_category(C++11)
generic_category(C++11)
system_category(C++11)
error_condition(C++11)
errc(C++11)
error_code(C++11)
system_error(C++11)
 
Defined in header <exception>
bool uncaught_exception();

Detects if the current thread has a live exception object, that is, an exception has been thrown and not yet entered a matching catch clause, std::terminate or std::unexpected. In other words, stack unwinding is currently in progress.

Any exception thrown while std::uncaught_exception() == true calls std::terminate.

Contents

[edit] Parameters

(none)

[edit] Return value

true if stack unwinding is currently in progress in this thread.

[edit] Exceptions

noexcept specification:  
noexcept
  (since C++11)

[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) [edit]
shared pointer type for handling exception objects
(typedef) [edit]

[edit] External links

GOTW issue 47: Uncaught Exceptions