I am trying to submit a task to an ExecutorService in Java. It either takes a Callable, which allows to throw an Exception, or it takes a Runnable. My use case is as foolows: I would like to schedule a task which throws an Exception, but is a void method. As a result I cannot use either Callable or Runnable, as the method definitions do not match my use case. I would also like to have my excepton propagated from the Future I receive after submission. Any ideas?
Share
You can use
Callable<Void>. Obviously you can’t instantiate a Void object, but just returnnull.From the
Future<Void>, you can still callget, discarding thenullreturn value. It will rethrow any exception as anExecutionException.