Skip to content

Latest commit

 

History

History
83 lines (67 loc) · 2.36 KB

nested_exception.md

File metadata and controls

83 lines (67 loc) · 2.36 KB

nested_exception

  • exception[meta header]
  • std[meta namespace]
  • class[meta id-type]
  • cpp11[meta cpp]
namespace std {
  class nested_exception;
}

概要

nested_exceptionは、例外を階層構造として処理する場合に、元の例外を一時的に保持した状態で、別の例外として送出するための使用する例外クラスである。

入れ子になった例外を捕捉した場合、rethrow_nested()メンバ関数を呼び出すことで、元の例外を送出できる。

メンバ関数

名前 説明 対応バージョン
(constructor) コンストラクタ C++11
~virtual nested_exception() = default; デストラクタ C++11
nested_exception& operator=(const nested_exception&) = default; 代入演算子 C++11
rethrow_nested 入れ子になった例外を送出する C++11
nested_ptr 入れ子になった例外へのポインタを取得する C++11

#include <exception>
#include <iostream>

class my_exception : public std::nested_exception {};

int main()
{
  try {
    try {
      try {
        throw 1; // int値を送出
      }
      catch (int& x) {
        std::cout << "1st caught: " << x << std::endl;
        throw my_exception(); // 現在の例外(int)を入れ子にしてmy_exceptionを送出
      }
    }
    catch (my_exception& e) {
      std::cout << "2nd caught: my_exception(nested_exception)." << std::endl;
      // 入れ子になっている例外(int)を送出
      e.rethrow_nested();
    }
  }
  catch (int& x) {
    std::cout << "3rd caught: " << x << std::endl;
  }
}
  • std::nested_exception[color ff0000]
  • e.rethrow_nested()[link nested_exception/rethrow_nested.md]

出力

1st caught: 1
2nd caught: my_exception(nested_exception).
3rd caught: 1

バージョン

言語

  • C++11

処理系

参照