[[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
処理系
- Clang: ??
- GCC: 4.6.1 ✅
- ICC: ??
- Visual C++: 2010 ✅, 2012 ✅, 2013 ✅, 2015 ✅
参照
- N2179 Language Support for Transporting Exceptions between Threads
- P1675R2
rethrow_exception
must be allowed to copy- 既存C++コンパイラの挙動にあわせて効果(Effects)文面を修正。