Difference between revisions of "cpp/error/rethrow exception"
(+) |
m (fmt.) |
||
(12 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
{{cpp/title|rethrow_exception}} | {{cpp/title|rethrow_exception}} | ||
− | {{cpp/error/ | + | {{cpp/error/navbar}} |
− | + | {{ddcl|header=exception|since=c++11| | |
− | {{ddcl | header=exception | | + | [[noreturn]] void rethrow_exception( std::exception_ptr p ); |
− | [[noreturn]] void rethrow_exception( std::exception_ptr p ) | + | |
}} | }} | ||
− | Throws the previously captured exception object | + | Throws the previously captured exception object referred-to by the exception pointer {{c|p}}, or a copy of that object. |
+ | |||
+ | It is unspecified whether a copy is made. If a copy is made, the storage for it is allocated in an unspecified way. | ||
+ | |||
+ | The behavior is undefined if {{c|p}} is null. | ||
===Parameters=== | ===Parameters=== | ||
− | {{ | + | {{par begin}} |
− | {{ | + | {{par|p|non-null {{lc|std::exception_ptr}}}} |
− | {{ | + | {{par end}} |
===Return value=== | ===Return value=== | ||
− | |||
(none) | (none) | ||
− | {{cpp/error/ | + | ===Exceptions=== |
+ | The exception object referred-to by {{c|p}} if no copy is made. | ||
+ | |||
+ | Otherwise, a copy of such exception object if the implementation successfully copied the exception object. | ||
+ | |||
+ | Otherwise, {{lc|std::bad_alloc}} or the exception thrown when copying the exception object, if allocation or copying fails, respectively. | ||
+ | |||
+ | ===Notes=== | ||
+ | Before {{wg21|P1675R2}}, {{tt|rethrow_exception}} was not allowed to copy the exception object, which is unimplementable on some platforms where exception objects are allocated on the stack. | ||
+ | |||
+ | ===Example=== | ||
+ | {{include|cpp/error/example_exception_ptr}} | ||
===See also=== | ===See also=== | ||
− | {{ | + | {{dsc begin}} |
− | {{ | + | {{dsc inc|cpp/error/dsc exception_ptr}} |
− | {{ | + | {{dsc inc|cpp/error/dsc current_exception}} |
− | {{ | + | {{dsc end}} |
+ | |||
+ | {{langlinks|de|es|fr|it|ja|pt|ru|zh}} |
Latest revision as of 09:48, 9 July 2024
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, or a copy of that object.
It is unspecified whether a copy is made. If a copy is made, the storage for it is allocated in an unspecified way.
The behavior is undefined if p is null.
Contents |
[edit] Parameters
p | - | non-null std::exception_ptr |
[edit] Return value
(none)
[edit] Exceptions
The exception object referred-to by p if no copy is made.
Otherwise, a copy of such exception object if the implementation successfully copied the exception object.
Otherwise, std::bad_alloc or the exception thrown when copying the exception object, if allocation or copying fails, respectively.
[edit] Notes
Before P1675R2, rethrow_exception
was not allowed to copy the exception object, which is unimplementable on some platforms where exception objects are allocated on the stack.
[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
(C++11) |
shared pointer type for handling exception objects (typedef) |
(C++11) |
captures the current exception in a std::exception_ptr (function) |