函式模板
<exception>

std::make_exception_ptr

template <class E>  exception_ptr make_exception_ptr (E e) noexcept;
建立 exception_ptr
返回一個指向 e 副本的 exception_ptr 物件。

此函式模板的行為等同於
1
2
3
4
5
6
7
template <class E> exception_ptr make_exception_ptr (E e) noexcept {
  try {
     throw e;
  } catch(...) {
     return current_exception();
  }
}

exception_ptr 是一種共享智慧指標型別:只要至少一個 exception_ptr 指向它,它所指向的異常就保證保持有效。更多資訊請參閱 exception_ptr

此函式不丟擲異常,但如果函式實現為返回指向當前處理的異常副本的指標,如果它無法分配儲存空間(bad_alloc)或複製過程失敗(如果可能,則返回丟擲的異常或 bad_exception;否則返回其他未指定的值),則它可能會返回指向不同異常的指標。

引數

e
一個物件或引用。

返回值

一個指向異常物件exception_ptr 物件,或者如果函式的內部過程會引發新異常,則指向另一個異常。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// make_exception_ptr example
#include <iostream>       // std::cout
#include <exception>      // std::make_exception_ptr, std::rethrow_exception
#include <stdexcept>      // std::logic_error

int main () {
  auto p = std::make_exception_ptr(std::logic_error("logic_error"));

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

輸出

exception caught: logic_error


資料競爭

返回的物件指向 e 的副本。

異常安全

無異常保證:此函式不丟擲異常。相反,此函式使用其返回值來指示其內部操作期間丟擲的異常。

另見