函式
<exception>

std::rethrow_exception

[[noreturn]] void rethrow_exception (exception_ptr p);
重新丟擲異常
丟擲由 p 指向的異常物件。

引數

p
一個指向異常物件的 exception_ptr 物件。
此引數不能是 *null* exception_ptr
exception_ptr 是一種指標型別的物件,它指向異常。

返回值

無(該函式永不返回)。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// exception_ptr example
#include <iostream>       // std::cout
#include <exception>      // std::exception_ptr, std::current_exception, std::rethrow_exception
#include <stdexcept>      // std::logic_error

int main () {
  std::exception_ptr p;
  try {
     throw std::logic_error("some logic_error exception");   // throws
  } catch(const std::exception& e) {
     p = std::current_exception();
     std::cout << "exception caught, but continuing...\n";
  }

  std::cout << "(after exception)\n";

  try {
     std::rethrow_exception (p);
  } catch (const std::exception& e) {
     std::cout << "exception caught: " << e.what() << '\n';
  }
  return 0;
}

輸出

exception caught, but continuing...
(after exception)
exception caught: some logic_error exception


資料競爭

併發呼叫指向同一異常的 exception_ptr 物件的 rethrow_exception 是安全的。

但是請注意,某些實現可能不會在進入 catch *異常處理塊*時複製指向的物件,在這種情況下併發訪問重新丟擲的異常物件可能會引入資料競爭。

異常安全

丟擲異常。

如果 p 是一個 *null* exception_ptr,則會導致 *未定義行為*。

另見