Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | error
m (~)
 
(7 intermediate revisions by 6 users not shown)
Line 1: Line 1:
 
{{cpp/title|exception_ptr}}
 
{{cpp/title|exception_ptr}}
 
{{cpp/error/navbar}}
 
{{cpp/error/navbar}}
{{ddcl | header=exception | since=c++11 | 1=
+
{{ddcl|header=exception|since=c++11|1=
typedef /*unspecified*/ exception_ptr;
+
using exception_ptr = /*unspecified*/
 
}}
 
}}
  
{{tt|std::exception_ptr}} is a nullable pointer-like type that manages an exception object which has been thrown and captured with {{lc|std::current_exception}}. An instance of {{tt|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.
+
{{tt|std::exception_ptr}} is a nullable pointer-like type that manages an exception object which has been thrown and captured with {{lc|std::current_exception}}. An instance of {{tt|std::exception_ptr}} may be passed to another function, possibly on another thread, where the exception may be rethrown and handled with a {{c/core|catch}} clause.
  
Default-constructed {{tt|std::exception_ptr}} is a null pointer, it does not point to an exception object.
+
A default-constructed {{tt|std::exception_ptr}} is a null pointer; it does not point to an 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.
 
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. It is convertible to {{tt|bool}}. false if it is a nullptr, true otherwise.
+
{{tt|std::exception_ptr}} is not implicitly convertible to any arithmetic, enumeration, or pointer type. It is contextually convertible to {{c/core|bool}}, and will evaluate to {{c|false}} if it is null, {{c|true}} otherwise.
  
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 (note: this is in addition to the usual [[cpp/language/throw#The exception object|exception object lifetime rules]]).
 +
 
 +
{{tt|std::exception_ptr}} meets the requirements of {{named req|NullablePointer}}.
  
 
===Example===
 
===Example===
{{include| cpp/error/example_exception_ptr}}
+
{{include|cpp/error/example_exception_ptr}}
  
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/error/dsc make_exception_ptr}}
+
{{dsc inc|cpp/error/dsc make_exception_ptr}}
{{dsc inc | cpp/error/dsc current_exception}}
+
{{dsc inc|cpp/error/dsc current_exception}}
{{dsc inc | cpp/error/dsc rethrow_exception}}
+
{{dsc inc|cpp/error/dsc rethrow_exception}}
 
{{dsc end}}
 
{{dsc end}}
  
[[de:cpp/error/exception ptr]]
+
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
[[es:cpp/error/exception ptr]]
+
[[fr:cpp/error/exception ptr]]
+
[[it:cpp/error/exception ptr]]
+
[[ja:cpp/error/exception ptr]]
+
[[pt:cpp/error/exception ptr]]
+
[[ru:cpp/error/exception ptr]]
+
[[zh:cpp/error/exception ptr]]
+

Latest revision as of 02:21, 9 July 2024

 
 
 
Defined in header <exception>
using exception_ptr = /*unspecified*/
(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.

A 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 contextually convertible to bool, and will evaluate to false if it is null, true otherwise.

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 (note: this is in addition to the usual exception object lifetime rules).

std::exception_ptr meets the requirements of NullablePointer.

[edit] 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)'

[edit] See also

creates an std::exception_ptr from an exception object
(function template) [edit]
captures the current exception in a std::exception_ptr
(function) [edit]
throws the exception from an std::exception_ptr
(function) [edit]