std::uncaught_exception - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::uncaught_exception

From cppreference.com
< cpp‎ | error

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, std::uncaught_exception detects if stack unwinding is currently in progress.

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.

### [edit] Parameters

(none)

### [edit] Return value

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

### [edit] Exceptions

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

### [edit] Example

Run this code

    #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

terminate | function called when exception handling fails (function) [edit]
exception_ptr (C++11) | shared pointer type for handling exception objects (typedef) [edit]

### [edit] External links

GOTW issue 47: Uncaught Exceptions

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/error/uncaught_exception&oldid=64831"
Category:

- unconditionally noexcept