Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Handling exceptions in child threads

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

like image 488
Zombies Avatar asked Apr 13 '10 17:04

Zombies


People also ask

What happens if a child thread throws exception?

Short answer, it doesn't. If the exception propagates all the way out of the thread, it'll simply die (possible generating some error print on the console).

How do you handle exceptions in multithreading in Java?

Uncaught exception handler will be used to demonstrate the use of exception with thread. It is a specific interface provided by Java to handle exception in the thread run method. There are two methods to create a thread: Extend the thread Class (java.

Can you catch an exception thrown by another thread in Java?

If all you have is a Thread object then there is no way to catch any exceptions (which I assume are RuntimeException ). The proper way to do this is by using the Future<?> class used by the ExecutorService but you don't have control over the code starting the Thread I assume.

What happens when exception occurs in thread?

If an unhandled exception occurs in the main thread of an application, the main thread terminates, along with your entire application. An unhandled exception in a spawned worker thread, however, will terminate only that thread.


2 Answers

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.

like image 64
John Vint Avatar answered Sep 29 '22 21:09

John Vint


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)

like image 44
Bill K Avatar answered Sep 29 '22 21:09

Bill K