LearnFrenzy
Sign in Take a free test

Concurrency and Executors · Predict the Output · Question 4

The cause of an ExecutionException Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 4 Back to Exercise

What is the output of the following Java program?

Main.java
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class Main {
    public static void main(String[] args) throws InterruptedException {
        try (ExecutorService pool = Executors.newFixedThreadPool(1)) {
            Future<Integer> f = pool.submit(() -> 10 / (args.length));
            try {
                System.out.println(f.get());
            } catch (ExecutionException e) {
                System.out.println(e.getCause().getClass().getSimpleName() + ": " + e.getCause().getMessage() + " | done=" + f.isDone());
            }
        }
    }
}
  • A ArithmeticException: / by zero | done=true Correct
  • B ExecutionException: / by zero | done=false
  • C 10
  • D ArithmeticException: / by zero | done=false
Correct Answer: Option A

With no command-line arguments the task divides by zero. The Future is done (exceptionally) and get() wraps the error.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links