Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | error
m (Use since= and until= params of {{ddcl}} template.)
(P1675R2, pretending it's a DR.)
Line 3: Line 3:
  
 
{{ddcl | header=exception | since=c++11 |
 
{{ddcl | header=exception | since=c++11 |
[[noreturn]] void rethrow_exception( std::exception_ptr p )
+
[[noreturn]] void rethrow_exception( std::exception_ptr p );
 
}}
 
}}
  
Throws the previously captured exception object, referred to by the exception pointer {{tt|p}}.
+
Throws the previously captured exception object referred-to by the exception pointer {{tt|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 {{tt|p}} is null.
  
 
===Parameters===
 
===Parameters===
Line 15: Line 19:
 
===Return value===
 
===Return value===
 
(none)
 
(none)
 +
 +
===Exceptions===
 +
The exception object referred-to by {{tt|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===
 
===Example===
Line 25: Line 39:
 
{{dsc end}}
 
{{dsc end}}
  
[[de:cpp/error/rethrow exception]]
+
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
[[es:cpp/error/rethrow exception]]
+
[[fr:cpp/error/rethrow exception]]
+
[[it:cpp/error/rethrow exception]]
+
[[ja:cpp/error/rethrow exception]]
+
[[pt:cpp/error/rethrow exception]]
+
[[ru:cpp/error/rethrow exception]]
+
[[zh:cpp/error/rethrow exception]]
+

Revision as of 03:12, 5 October 2021

 
 
 
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

Parameters

p - non-null std::exception_ptr

Return value

(none)

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.

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.

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

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