Question

I would like to know how to handle exceptions from my Callable when I use Void as a return type.

Something like this:

ExecutorService executor = Executors.newSingleThreadExecutor();
executor.submit(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            call a third party code where an exception is thrown...
            return null;
        }

});

Since I don't need any result from this callable I don't call get() on the Future returned from calling executor.submit(). Thus the exception is swallowed.

What is the right way to handle such exceptions?

Was it helpful?

Solution

How about try/catch within your call() method? - I suppose it depends on what you want to do if an exception occurs.

public Void call() throws Exception {
    try {
        ThirdParty.doSomething();
    } catch(SomeTypeException e) {
        SomeErrorHandler.handleThisError(e); // E.g. report it to the user
    }
    return null;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top