##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::rethrow_exception

From cppreference.com
< cpp‎ | error

Defined in header `<exception>` |  |
[[noreturn]] void rethrow_exception( std::exception_ptr p ) |  | (since C++11)

Throws the previously captured exception object, referred to by the exception pointer `p`.

### [edit] Parameters

p | - | non-null std::exception_ptr

### [edit] Return value

(none)

### [edit] Example

[edit]
Run this code

    #include <iostream>
    #include <string>
    #include <exception>
    #include <stdexcept>

    void handle_eptr(std::exception_ptr eptr) // passing by value is ok
    {
        try {
            if (eptr != std::exception_ptr()) {
                std::rethrow_exception(eptr);
            }
        } catch(const std::exception& e) {
            std::cout << "Caught exception \"" << e.what() << "\"\n";
        }
    }

    int main()
    {
        std::exception_ptr eptr;
        try {
            std::string().at(1); // this generates an std::out_of_range
        } catch(...) {
            eptr = std::current_exception(); // capture
        }
        handle_eptr(eptr);
    } // destructor for std::out_of_range called here, when the eptr is destructed

Output:

    Caught exception "basic_string::at"

### [edit] See also

exception_ptr (C++11) | shared pointer type for handling exception objects (typedef) [edit]
current_exception (C++11) | captures the current exception in a std::exception_ptr (function) [edit]

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