Reputation: 309
I thought Java Errors were indications of serious problems and shouldn't be handled. Why then, does this code run fine?
public static void main(String[] args)
{
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<?> future = executor.submit(() -> {throw new AssertionError();});
while (!future.isDone()) {
}
System.out.println("done");
}
I had an unimplemented method which threw an AssertionError to remind me to implement it but it just got swallowed up with absolutely no indication that something was seriously wrong.
Upvotes: 2
Views: 803
Reputation:
It's not swallowed, it's there:
public static void main(String[] args) {
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<?> future = executor.submit(() -> {
throw new AssertionError();
});
while (!future.isDone()) {
}
try {
future.get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
System.out.println("done");
}
You get the error on calling get method that gives you the result of computation if succeeded or an error otherwise.
java.util.concurrent.ExecutionException: java.lang.AssertionError at java.util.concurrent.FutureTask.report(FutureTask.java:122) at java.util.concurrent.FutureTask.get(FutureTask.java:192) ...
Upvotes: 6