◐ Shell
clean mode source ↗

std::rethrow_exception - cppreference.com

提供: cppreference.com

<tbody> </tbody>

[[noreturn]] void rethrow_exception( std::exception_ptr p )

(C++11以上)

例外ポインタ p によって参照されている、以前にキャプチャされた例外オブジェクトを投げます。

引数

p - ヌルでない std::exception_ptr

戻り値

(なし)

#include <iostream>
#include <string>
#include <exception>
#include <stdexcept>

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 {
        std::string().at(1); // this generates an 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

出力:

Caught exception "basic_string::at"

関連項目