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
The most common approach is to submit a
Callableto anExecutor, and receive aFuture. You can then callFuture.getto find out if any exceptions were thrown.As for
UncaughtExceptionHandler, you can only really use it with raw threads becauseExecutors 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 overExecutors,Callableis IMO the best bet.