std:: exception_ptr
|
ヘッダーで定義
<exception>
|
||
|
using
exception_ptr
=
/*unspecified*/
|
(C++11以降) | |
std::exception_ptr
は、スローされ
std::current_exception
で捕捉された例外オブジェクトを管理する、nullableなポインタ様の型です。
std::exception_ptr
のインスタンスは、他の関数(おそらく別のスレッド)に渡すことができ、そこで例外を再スローして
catch
節で処理することが可能です。
デフォルト構築された
std::exception_ptr
はヌルポインタです。これは例外オブジェクトを指していません。
std::exception_ptr
の2つのインスタンスは、両方がnullであるか、または両方が同じ例外オブジェクトを指している場合にのみ等価となります。
std::exception_ptr
は算術型、列挙型、ポインタ型のいずれにも暗黙的に変換されません。これは
bool
への文脈的変換が可能であり、nullの場合は
false
に、それ以外の場合は
true
と評価されます。
std::exception_ptr
によって参照される例外オブジェクトは、それを参照する
std::exception_ptr
が少なくとも1つ存在する限り有効なままです:
std::exception_ptr
は共有所有権スマートポインタです(注:これは通常の
例外オブジェクトの寿命ルール
に追加されるものです)。
std::exception_ptr
は
NullablePointer
の要件を満たします。
例
#include <exception> #include <iostream> #include <stdexcept> #include <string> void handle_eptr(std::exception_ptr eptr) // 値渡しで問題ありません { 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); // これは std::out_of_range を生成します } catch(...) { eptr = std::current_exception(); // 例外をキャプチャ } handle_eptr(eptr); } // eptr が破棄されるときに、ここで std::out_of_range のデストラクタが呼び出されます
出力例:
Caught exception: 'basic_string::at: __n (which is 1) >= this->size() (which is 0)'
関連項目
|
(C++11)
|
例外オブジェクトから
std::exception_ptr
を作成する
(関数テンプレート) |
|
(C++11)
|
現在の例外を
std::exception_ptr
にキャプチャする
(関数) |
|
(C++11)
|
std::exception_ptr
から例外をスローする
(関数) |