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).
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:
This will give you both stack traces.
(Thanks Taylor L)