6

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++).

  • Just wanted to make sure I didn't get answers that are specific for C# for instance :) –  Dec 12 '09 at 16:22
  • 1
    Some of the solutions are most definitely language/platform specific. C++ and C# exceptions do differ, and C++ exceptions are implemented differently on different platforms. – Assaf Lavie Dec 12 '09 at 17:44
  • A good solution is described in this [stack overflow thread](http://stackoverflow.com/questions/23820633/gdb-prevent-losing-backtrace-in-a-catch-rethrow-situation) – Andrea Araldo Feb 13 '17 at 19:26
  • The solution described in this [stackoverflow post](http://stackoverflow.com/questions/23820633/gdb-prevent-losing-backtrace-in-a-catch-rethrow-situation) may be very useful for your needs. – Andrea Araldo Feb 13 '17 at 19:34

2 Answers2

2

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
  • 33,034
  • 36
  • 130
  • 210
0

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
  • 73,079
  • 34
  • 148
  • 203
  • 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) –  Dec 12 '09 at 18:11