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?
You can use Callable<Void>
. Obviously you can't instantiate a Void object, but just return null
.
From the Future<Void>
, you can still call get
, discarding the null
return value. It will rethrow any exception as an ExecutionException
.