views:

253

answers:

3

I prefer to have the exception handling logic further up in the call stack, near the main method. I like this approach... However, I created a thread where some of its method calls inside run() may throw exceptions. I would really like to see if there is a way that these exceptions can be thrown back up to the parent thread? The best I could think of is setting a variable inside the object that implements Runnable. This variable is a string that contains the Error Message, which then uses a class loader to correctly re-create the same exception in the parent thread.

What I would like to know, is there a less messy way of getting what I want here? (to be able to make sure that any exception thrown in a child thread is handled with the same exception handling logic as though it was running in the main thread/code re-use).

A: 

Catch it at the outer level of your run() method then place the Exception in a variable in your Runnable and have your Runnable indicate that it completed.

The code that started your runnable has to then examine the Runnable to see that the "Exception" object is set and either rethrow it or deal with it.

If you rethrow it, you may want to wrap it in a new exception:

throw new Exception(oldException);

This will give you both stack traces.

(Thanks Taylor L)

Bill K
I think fillInStackTrace would replace the current stack frame rather than the original stack frame of the exception. I'm not sure that's what you'd want. Perhaps it would be better to wrap the exception with a new one like throw new YourException(originalException). The rest seems fine though.
Taylor Leese
Oh haha, of course lol. No need for a complex class loader.
Zombies
+9  A: 

You can use an ExecutorService here to submit a callable and receive a Future. At the point you would at the very least want the Exception to be propagated to the invoking thread you would invoke future.get()

future.get() will propogate any exception thrown in the call method to the thread that is invoking future.get(). So if your main thread invokes future.get() then the main thread will see any exceptions thrown.

John V.
This is great! I didn't know it was possible this way.
pajton
Futures is one of the most under-used features in Java. More than half of the direct thread usage I need can be done safer, easier, and cheaper with Futures.
James Schek
@James youre 100% right, even a simple synchronization barrier is much easier with Futures then the more known CountdownLatch and CyclicBarrier
John V.
+1  A: 

With Thread.setUncaughtExceptionHandler() you can set an Thread.UncaughtExceptionHandler in your Thread and have a centralized logic of handling exceptions of your threads.

Moreover you can write your own ThreadFactory that will create you threads with preset Thread.UncaughtExceptionHandler.

pajton
I think my only issue with that is that `Thread.UncaughtExceptionHandler` forces me to handle the exception there, but I want to throw it back up a few stacks towards main().
Zombies