std::rethrow_exception
From cppreference.com
| ヘッダー <exception> で定義 |
||
[[noreturn]] void rethrow_exception( std::exception_ptr p ); |
(C++11以降) (C++26 から constexpr) |
|
例外ポインタ p が参照する、以前に捕捉された例外オブジェクト、またはそのオブジェクトのコピーを再スローします。
コピーが作成されるかどうかは未指定です。コピーが作成される場合、そのストレージは未指定の方法で割り当てられます。
p がヌルの場合、動作は未定義です。
目次 |
[編集] パラメータ
| p | - | null でない std::exception_ptr |
[編集] 例外
コピーが作成されない場合は、p が参照する例外オブジェクト。
それ以外の場合、実装が例外オブジェクトのコピーに成功した場合は、そのような例外オブジェクトのコピー。
それ以外の場合、割り当てまたはコピーがそれぞれ失敗した場合は、std::bad_alloc または例外オブジェクトのコピー時にスローされた例外。
[編集] 注釈
P1675R2 以前は、`rethrow_exception` は例外オブジェクトをコピーすることは許されていませんでしたが、これは一部のプラットフォームでは例外オブジェクトがスタックに割り当てられるため実装不可能でした。
| 機能テストマクロ | 値 | 規格 | 機能 |
|---|---|---|---|
__cpp_lib_constexpr_exceptions |
202411L |
(C++26) | constexpr for exception types |
[編集] 例
このコードを実行
#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
実行結果の例
Caught exception: 'basic_string::at: __n (which is 1) >= this->size() (which is 0)'
[編集] 関連項目
| (C++11) |
例外オブジェクトを扱うための共有ポインタ型 (typedef) |
| (C++11) |
現在の例外を std::exception_ptr にキャプチャする (関数) |