Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception during Callable execution

I have the following Callable:

public class Worker implements Callable<Boolean>{

   @Override
   public Boolean call(){
      boolean success=true;

      //do Something
     return success;
   }

}

Now I'm executing it:

Worker worker - new Worker();
Future<Boolean> submit = executor.submit(worker);

I'm storing the submit in kind of hashMap for some operation to be performed somewhere in code.

How can I know if any exception has occured in worker.call() function?

Will submit.isCancelled() return true if some sort of Exception occurred and false if everything works ok?

like image 701
danny.lesnik Avatar asked Dec 07 '11 15:12

danny.lesnik


2 Answers

When you call Future.get() it will throw your exception wrapped in a ExecutionException.

like image 166
Victor Sorokin Avatar answered Sep 28 '22 13:09

Victor Sorokin


As stated in the documentation, Future<V> will re-throw the exception that occurred during .call() (though wrapped in an ExecutionException), and isCancelled() would still be false in this case.

like image 27
Romain Avatar answered Sep 28 '22 14:09

Romain