最終更新日時(UTC):
が更新

履歴 編集

function
<exception>

std::rethrow_exception(C++11)

[[noreturn]] void rethrow_exception(exception_ptr p);

概要

exception_ptrが指す例外オブジェクトを再スローする。

事前条件

pがヌルを指すexception_ptrではないこと。

効果

説明用の変数uを、pが指す例外オブジェクトもしくはそのコピーとする。 コピーが行われるか否か、コピー時にメモリ確保が行われるか否かは未規定とされる。

  • u用のメモリ確保に失敗した場合、bad_alloc例外がスローされる。
  • そうでなければ、pが指す例外オブジェクトからuへのコピー時に例外スローされた場合、その例外がスローされる。
  • そうでなければ、throw u;

戻り値

この関数は決して返らない。

#include <iostream>
#include <exception>
#include <stdexcept>

int main()
{
  std::exception_ptr ep;

  try {
    throw std::runtime_error("error!");
  }
  catch (...) {
    std::cout << "catch" << std::endl;
    ep = std::current_exception(); // 処理中の例外ポインタを取得
  }

  if (ep) {
    std::cout << "rethrow" << std::endl;
    std::rethrow_exception(ep); // 再スロー
  }
}

出力例

catch
rethrow

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
terminate called after throwing an instance of 'std::runtime_error'
  what():  error!

バージョン

言語

  • C++11

処理系

参照