A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://en.cppreference.com/w/cpp/language/../language/../error/rethrow_exception.html below:

std::rethrow_exception - cppreference.com

(since C++11)
(constexpr since C++26)

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.

[edit] Parameters [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

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4