名前空間
変種
操作

std::exception_ptr

提供: 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)
 
エラー処理
例外処理
exception_ptr
(C++11)
例外処理の失敗
(C++17未満)
(C++17未満)
(C++11)(C++17未満)
(C++17未満)
エラー番号
エラー番号
 
ヘッダ <exception> で定義
typedef /*unspecified*/ exception_ptr;
(C++11以上)

std::exception_ptr は、投げられて std::current_exception によってキャプチャされた例外オブジェクトを管理する��ル可能なポインタライクな型です。 std::exception_ptr のインスタンスは別の関数や別のスレッドに渡し、投げ直して catch 節で処理することができます。

デフォルト構築された std::exception_ptr はヌルポインタであり、例外オブジェクトを指しません。

std::exception_ptr の2つのインスタンスは、それらがどちらもヌルであるか、どちらも同じ例外オブジェクトを指す場合にのみ、等しいと比較されます。

std::exception_ptr はいかなる算術型、列挙型、ポインタ型にも暗黙に��換することはできません。 bool には文脈的に変換することができ、ヌルであれば false に、そうでなければ true に評価されます。

std::exception_ptr によって参照される例外オブジェクトは、それを参照している std::exception_ptr が少なくともひとつ残っている限り、有効なままです。 std::exception_ptr は共有所有権を持つスマートポインタです (ノート: これは通常の例外オブジェクトの生存期間のルールに追加されます)。

std::exception_ptrNullablePointer の要件を満たします。

[編集]

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

[編集] 関連項目

例外オブジェクトから std::exception_ptr を作成します
(関数テンプレート) [edit]
現在の例外を std::exception_ptr にキャプチャします
(関数) [edit]
std::exception_ptr から例外を投げます
(関数) [edit]