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.
You're looking for
You can get the currently-caught exception using
std::current_exception(), store the resulting
std::exception_ptr, and throw it later with
There is a good example of usage on the cppreference wiki.