std:: rethrow_exception
|
ヘッダーで定義
<exception>
|
||
|
[
[
noreturn
]
]
void
rethrow_exception
(
std::
exception_ptr
p
)
;
|
(C++11以降)
(C++26以降constexpr) |
|
例外ポインタ p によって参照される、事前に捕捉された例外オブジェクト、またはそのオブジェクトのコピーをスローします。
コピーが作成されるかどうかは未規定です。コピーが作成される場合、そのためのストレージは未規定の方法で割り当てられます。
p が null の場合、動作は未定義です。
目次 |
パラメータ
| p | - | 非null std::exception_ptr |
例外
コピーが作成されない場合、 p によって参照される例外オブジェクト。
それ以外の場合、実装が例外オブジェクトのコピーに成功した場合は、その例外オブジェクトのコピー。
それ以外の場合、 std::bad_alloc または例外オブジェクトのコピー時にスローされる例外が、それぞれアロケーションまたはコピーが失敗した場合にスローされます。
注記
Before
P1675R2
、
rethrow_exception
は例外オブジェクトのコピーを許可しておらず、これは例外オブジェクトがスタック上に割り当てられる一部のプラットフォームでは実装不可能でした。
| 機能テスト マクロ | 値 | 標準 | 機能 |
|---|---|---|---|
__cpp_lib_constexpr_exceptions
|
202411L
|
(C++26) | constexpr 例外型のためのconstexpr |
例
#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
にキャプチャする
(function) |