function template
<exception>

std::make_exception_ptr

template <class E>
  exception_ptr make_exception_ptr (E e) noexcept;
Make exception_ptr
Returns an exception_ptr object that points to a copy of e.

The behavior of this function template is equivalent to:
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 is a shared smart pointer type: The pointed exception is guaranteed to remain valid for as long as at least one exception_ptr points to it. See exception_ptr for more info.

This function throws no exceptions, but if the function is implemented as returning a pointer to a copy of the currently handled exception, it may return a pointer to a different exception if it fails to allocate storage (bad_alloc) or if the copying process fails (it returns the thrown exception or bad_exception, if possible; or some other unspecified value, otherwise).

Parameters

e
An object or reference.

Return value

An exception_ptr object pointing to the an exception object, or some other exception if the internal process of the function would raise a new exception.

Example

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;
}


Output:

exception caught: logic_error

Data races

The returned object refers to a copy of e.

Exception safety

No-throw guarantee: this function throws no exceptions. Instead, this function uses its return value to signal exceptions thrown during its internal operation.

See also