Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | error
m (Text replace - "cpp/error/exception" to "cpp/error")
m (Text replace - "/sidebar" to "/navbar")
Line 1: Line 1:
 
{{cpp/title|rethrow_exception}}
 
{{cpp/title|rethrow_exception}}
{{cpp/error/sidebar}}
+
{{cpp/error/navbar}}
  
 
{{ddcl | header=exception | notes={{mark since c++11}} |
 
{{ddcl | header=exception | notes={{mark since c++11}} |

Revision as of 12:33, 15 June 2012

 
 
 
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.

Contents

Parameters

p - non-null std::exception_ptr

Return value

(none)

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 exception ptrTemplate:cpp/error/dcl list current exception