views:

294

answers:

2

The situation is as follows: Thread A catches an exception, saves the exception's data somewhere in memory (using GetExceptionInformation in the exception filter), and afterwords Thread B gets that exception information and wants to rethrow it. But the thing is, when thread B rethrows the caught exception, i'm missing the original call stack that lead to the exception.
How can I rethrow the exception without losing the original call stack? (note that this question is about C++).

+2  A: 

You could unwind the stack in the catch block and save it as part of the exception you are rethrowing. Unwinding the stack in C++ is a bit tricky, but you could have a al look at the crashdump collector code that comes with WxWidgets for an example.

Adrian Grigore
marked as answer though not entirely practical in my case
A: 

Question is why would you need to pass the stack to the "receiving" thread.

I assume you need the stack in order to basically report it to some error log. You can walk the stack in the catching thread, or produce a mini dump, or whatever error info you wish to collect, and then just pass a copy of the exception (if possible, beware of slicing) to the receiving thread.

Assaf Lavie
the exception i caught inside a kind of a fork-join routine. so if an exception is thrown in some worker thread, i'd like to pass it to the main thread and rethrow it there (+ i don't want to lose the original stack)