What if you need to catch an exception in a worker thread and re-throw it in the main thread that’s waiting for the worker to finish? std::future works this way. If you spawn a future on a new thread using std::async(std::launch::async, ...); and that future’s worker throws an exception, when you later call get() on the future it will emit that exception.

You do it by wrapping the worker thread’s function in try { /* CODE */ } catch(...) {} and capturing the current exception pointer ( std::exception_ptr) using std::current_exception. You can then re-throw the captured exception using the pointer and std::rethrow_exception. Below is an example that illustrates this technique. Just remember, if you have multiple worker threads make sure to have multiple std::exception_ptr instances; one per worker thread.

exceptions.cpp:

Thread 0x1048d75c0 caught exception from thread 0x700001cf3000

Program output.

2 Replies to “Propagate exceptions across threads”

  1. One common problem with doing this (or capturing and rethrowing in general) is that you lose the original thread’s stack when the main thread eventually cores.

    I like to core programmatically before unwinding because of this.

Leave a Reply