Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/error/exception ptr"

From cppreference.com
< cpp‎ | error
m (Text replace - "/sidebar" to "/navbar")
Line 11: Line 11:
 
Two instances of {{tt|std::exception_ptr}} compare equal only if they are both null or both point at the same exception object.
 
Two instances of {{tt|std::exception_ptr}} compare equal only if they are both null or both point at the same exception object.
  
{{tt|std::exception_ptr}} is not implicitly convertible to any arithmetic, enumeration, or pointer type.
+
{{tt|std::exception_ptr}} is not implicitly convertible to any arithmetic, enumeration, or pointer type. It is convertible to {{tt|bool}}.
  
 
The exception object referenced by an {{tt|std::exception_ptr}} remains valid as long as there remains at least one {{tt|std::exception_ptr}} that is referencing it: {{tt|std::exception_ptr}} is a shared-ownership smart pointer.
 
The exception object referenced by an {{tt|std::exception_ptr}} remains valid as long as there remains at least one {{tt|std::exception_ptr}} that is referencing it: {{tt|std::exception_ptr}} is a shared-ownership smart pointer.

Revision as of 06:16, 17 June 2012

 
 
 
Defined in header <exception>
typedef /*unspecified*/ exception_ptr;
(since C++11)

std::exception_ptr is a nullable pointer-like type that manages an exception object which has been thrown and captured with std::current_exception. An instance of std::exception_ptr may be passed to another function, possibly on another thread, where the exception may be rethrown and handled with a catch clause.

Default-constructed std::exception_ptr is a null pointer, it does not point to an exception object.

Two instances of std::exception_ptr compare equal only if they are both null or both point at the same exception object.

std::exception_ptr is not implicitly convertible to any arithmetic, enumeration, or pointer type. It is convertible to bool.

The exception object referenced by an std::exception_ptr remains valid as long as there remains at least one std::exception_ptr that is referencing it: std::exception_ptr is a shared-ownership smart pointer.

Example

#include <exception>
#include <iostream>
#include <stdexcept>
#include <string>
 
void handle_eptr(std::exception_ptr eptr) // passing by value is OK
{
    try
    {
        if (eptr)
            std::rethrow_exception(eptr);
    }
    catch(const std::exception& e)
    {
        std::cout << "Caught exception: '" << e.what() << "'\n";
    }
}
 
int main()
{
    std::exception_ptr eptr;
 
    try
    {
        [[maybe_unused]]
        char ch = std::string().at(1); // this generates a 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

Possible output:

Caught exception: 'basic_string::at: __n (which is 1) >= this->size() (which is 0)'

See also

Template:cpp/error/dcl list make exception ptrTemplate:cpp/error/dcl list current exceptionTemplate:cpp/error/dcl list rethrow exception