Java 5 Multi threading, catch thread exceptions

166 views Asked by At

I've got a class that generates threads (file i/o). I need to catch exceptions in the thread - I don't want to do anything fancy, I want to kill the main thread, rather, stop processing altogether so it can start over.

If I catch the exceptions in the thread, that's all good and well but the caller still proceeds, I want it to die.

I've had a few ideas but nothing is really getting me anywhere. What's the most succinct way to do this-CallBack, UnCaughtExceptionHandler? Something else? I am also relegated to Java 5

1

There are 1 answers

0
Enno Shioji On BEST ANSWER

The most common approach is to submit a Callable to an Executor, and receive a Future. You can then call Future.get to find out if any exceptions were thrown.

As for UncaughtExceptionHandler, you can only really use it with raw threads because Executors handle uncaught exceptions internally (thus you don't even see the uncaught exceptions in the handler!), and since you rarely want to use raw threads over Executors, Callable is IMO the best bet.