remdezx remdezx - 1 year ago 144
C++ Question

Rethrowing typed exception in C++ or C++11

I need to write a wrapper to a function. Its purpose is to cache the result and possibly exceptions of the function. It should work as follows: during the first execution it should execute the function and store the result (or exception) returned by the function. During next executions it should return previously stored result or rethrow caught exception.

The problem is, that I want to store the type of exception. So basically I want to catch every type of exception, and rethrow it in the future without losing its type.

I'm using C++ or C++11, so any solution in one of these languages would be very appreciated.

Answer Source

You're looking for std::exception_ptr.

You can get the currently-caught exception using std::current_exception(), store the resulting std::exception_ptr, and throw it later with std::rethrow_exception(std::exception_ptr)

There is a good example of usage on the cppreference wiki.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download