std::current_exception
提供: cppreference.com
ヘッダ <exception> で定義
|
||
std::exception_ptr current_exception() noexcept; |
(C++11以上) | |
例外処理中 (一般的には catch 節の中) で呼ばれた場合は、現在の例外オブジェクトをキャプチャし、その例外オブジェクトのコピーまたは参照 (実装に依存します) を保持する std::exception_ptr を作成します。 参照されたオブジェクトは少なくともそれを参照する exception_ptr
オブジェクトが存在する限り有効なままです。
この関数の実装が new の呼���出しを必要としてその呼び出しが失敗した場合、返されたポインタは std::bad_alloc のインスタンスへの参照を保持します。
この関数の実装がキャプチャした例外オブジェクトのコピーを必要としてそのコピーコンストラクタが例外を投げた場合、返されたポインタはその投げられた例外への参照を保持します。 その投げられた例外オブジェクトのコピーコンストラクタも例外を投げた場合、返されたポインタはエンドレスループを中断するために std::bad_exception のインスタンスへの参照を保持することがあります。
処理中の例外がないときにこの関数が呼ばれた場合は、空の std::exception_ptr が返されます。
目次 |
[編集] 引数
(なし)
[編集] 戻り値
例外オブジェクト、または例外オブジェクトのコピー、または std::bad_alloc のインスタンスまたは std::bad_exception のインスタンスへの参照を保持する std::exception_ptr のインスタンス。
[編集] 例
Run this code
#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"
[編集] 関連項目
(C++11) |
例外オブジェクトを扱うための共有ポインタ型 (typedef) |
(C++11) |
std::exception_ptr から例外を投げます (関数) |
(C++11) |
例外オブジェクトから std::exception_ptr を作成します (関数テンプレート) |
(C++20で削除)(C++17) |
例外処理が現在進行中かどうか調べます (関数) |