views:

78

answers:

1

Hi,

I have a main application with a global exception handler installed. Now, for some specific exceptions being raised within another thread I want the global exception handler to be invoked. But it does only handle exceptions from the main thread. I also tried the following from within the thread but it does not work either:

RunInMainThread (procedure
                 begin
                 raise EExceptionFromWithinThread.Create;
                 end);

where RunInMainThread just executes the anonymous method given as a parameter in the context of the main thread.

Why doesn't this work? What's the proper way to handle such a situation?

+3  A: 

How about this: send a message to the main thread which exception should be raised.

splash
This is a very good idea. Very good.
Andreas Rejbrand
Thanks, I'll try that. Any explanation why it doesn't work with `Synchronize` (being used internally by `RunInMainThread`)?
Smasher
@Smasher: nope, no idea :-(
splash
Very good idea. I would add though that it may be a good idea to use specific exception classes for this situation and to add the thread idea to message posted to the main thread. Both in order to distinguish these exceptions from exceptions raised in the main thread itself.
Marjan Venema
+1 works fine. Thanks again!
Smasher