If I run a thread in a ExecutorService
is there a way to know that this thread did not throw an exception when it started execution?
As per the JavaDoc, you can submit your runnable the the executor using submit()
ExecutorService service = Executors.newSingleThreadExecutor();
Future f = service.submit(new Runnable() {
@Override
public void run() {
throw new RuntimeException("I failed for no reason");
}
});
try {
f.get();
} catch (ExecutionException ee) {
System.out.println("Execution failed " + ee.getMessage());
} catch (InterruptedException ie) {
System.out.println("Execution failed " + ie.getMessage());
}
This method will only work when your exceptions are unchecked. If you have checked exceptions, rethrow them wrapped in a RuntimeException
or use the Callable
instead of Runnable
interface.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With