Handling native cross-thread exceptions

G

Guest

Hello,

I am a C++ newbie. I developed a Win32 DLL that creates a number of threads,
which can throw exceptions. When an exception is thrown on a thread in Win32
DLL, I want to catch that exception on the main thread.

Is there any way to throw an exception on one thread and catch it on another
thread?

If #1 is not possible, is there a catch-all exception handler at the process
level in C++?

Thank you very much for your help,

Evgueni
 
C

Carl Daniel [VC++ MVP]

Zhenya said:
Hello,

I am a C++ newbie. I developed a Win32 DLL that creates a number of
threads, which can throw exceptions. When an exception is thrown on a
thread in Win32 DLL, I want to catch that exception on the main
thread.

Is there any way to throw an exception on one thread and catch it on
another thread?

Overtly, no, it's not possibble. Exceptions always propagate and are caught
(or not) on the thread where the exception was raised.
If #1 is not possible, is there a catch-all exception handler at the
process level in C++?

The effect of an unhandled exception is to terminate the process. You can
change that behavior by using the Win32 function
SetUnhandledExceptionFilter. Generally, you shouldn't do anything other
than possibly inform the user that the program must terminate and then
terminate the process yourself. If you want to produce a crash dump, this
is best handled by associating a custom debugger (or Dr Watson) with your
executable and having that debugger produce the crash dump. Such a debugger
runs as a separate process launched when your main program crashes, so you
don't have to worry about the state of the stack, heap, or code image of the
crashed process - it can be completely trashed and the debugger can still
produce a crash dump - something that you cannot be guaranteed of doing from
inside the crashed process itself.

-cd
 
B

Ben Voigt [C++ MVP]

Carl Daniel said:
Overtly, no, it's not possibble. Exceptions always propagate and are
caught (or not) on the thread where the exception was raised.

Of course one option is to catch it on the same thread and pass a message
(PostMessage) to the main thread.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top