名前空間
変種
操作

std::current_exception

提供: cppreference.com
< cpp‎ | error
 
 
ユーティリティライブラリ
汎用ユーティリティ
日付と時間
関数オブジェクト
書式化ライブラリ (C++20)
(C++11)
関係演算子 (C++20で非推奨)
整数比較関数
(C++20)
スワップと型操作
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
一般的な語彙の型
(C++11)
(C++17)
(C++17)
(C++17)
(C++17)

初等文字列変換
(C++17)
(C++17)
 
エラー処理
例外処理
current_exception
(C++11)
例外処理の失敗
(C++17未満)
(C++17未満)
(C++11)(C++17未満)
(C++17未満)
エラー番号
エラー番号
 
ヘッダ <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 のインスタンス。

[編集]

#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"

[編集] 関連項目

例外オブジェクトを扱うための共有ポインタ型
(typedef) [edit]
std::exception_ptr から例外を投げます
(関数) [edit]
例外オブジェクトから std::exception_ptr を作成します
(関数テンプレート) [edit]
例外処理が現在進行中かどうか調べます
(関数) [edit]