How to properly catch RuntimeExceptions from Executors?

The clean workaround is to use ExecutorService.submit() instead of execute(). This returns you a Future which you can use to retrieve the result or exception of the task:

ExecutorService executor = Executors.newSingleThreadExecutor();
Runnable task = new Runnable() {
  public void run() {
    throw new RuntimeException("foo");

Future<?> future = executor.submit(task);
try {
} catch (ExecutionException e) {
  Exception rootException = e.getCause();

Leave a Comment