Skip to content

Instantly share code, notes, and snippets.

@aozturk
Last active November 19, 2015 14:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aozturk/3cf1b38602a8ce494717 to your computer and use it in GitHub Desktop.
Save aozturk/3cf1b38602a8ce494717 to your computer and use it in GitHub Desktop.
final class MyTask implements Runnable {
@Override
public void run() {
System.out.println("My task is started running...");
// ...
throw new ArithmeticException();
// ...
}
}
public class FutureGetHandler {
public static void main(String[] args) {
ExecutorService threadPool = Executors.newFixedThreadPool(1);
Future<?> future = threadPool.submit(new MyTask());
try {
future.get();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (ExecutionException e) {
// Extract the actual exception from its wrapper
Throwable t = e.getCause();
System.err.println("Uncaught exception is detected! " + t
+ " st: " + Arrays.toString(t.getStackTrace()));
// ... Handle the exception
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment