Sub topic 9
What Happens When a Task Throws
In the last chapter you used Future.get() to read a task's result. Tasks don't always succeed, though. A task might divide by zero, read a missing file, or try to parse bad input. This chapter shows where that error ends up and how to catch it.
The error does not jump to your code
An exception is an object Java throws when something goes wrong. In a normal single-threaded program, the exception travels up to the method that called the failing code.
A task in a thread pool runs on a different thread. That thread is a worker thread, a thread owned by the pool. Your main method never called the task directly, so the exception has no direct path back to main.
When you hand a task to the pool with submit, the pool catches the exception for you and stores it inside the task's Future. Nothing is printed and nothing crashes. The error just waits there.
A failure you never see
This program submits a task that throws. Run it and look at the output.
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class SilentFailureDemo {
public static void main(String[] args) {
ExecutorService pool = Executors.newSingleThreadExecutor();
Runnable task = () -> {
throw new IllegalStateException("Something broke");
};
pool.submit(task);
pool.shutdown();
System.out.println("Main thread done");
}
}The only output is Main thread done. The IllegalStateException happened, but the program never asked the Future about it, so you never see it.
Getting the error back with ExecutionException
When you call get() on a Future whose task failed, get() does not return a value. It throws an ExecutionException, a checked exception that wraps the real error. Call getCause() on it to get the original exception.
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class TaskFailureDemo {
public static void main(String[] args) throws InterruptedException {
ExecutorService pool = Executors.newSingleThreadExecutor();
try {
Callable<Integer> failing = () -> Integer.parseInt("not a number");
Future<Integer> result = pool.submit(failing);
result.get();
} catch (ExecutionException e) {
System.out.println("Task failed: " + e.getCause());
} finally {
pool.shutdown();
}
}
}Output:
Task failed: java.lang.NumberFormatException: For input string: "not a number"Here is what happens, step by step:
- The worker thread runs
Integer.parseInt("not a number"), which throwsNumberFormatException. - The pool catches it and stores it in the
Future. maincallsresult.get().get()throwsExecutionException, with theNumberFormatExceptionas its cause.- The
catchblock prints the cause.
submit versus execute
Executors offer two ways to hand over a task, and they deal with errors differently.
| Method | Returns | Where a task's exception goes |
|---|---|---|
| `submit(task)` | a `Future` | Stored in the `Future`. You see it only when you call `get()`. |
| `execute(task)` | nothing | Kills that worker thread and is printed to the error output. The pool creates a new worker. |
Exercises
Exercise
A program submits a task with pool.submit(...), never calls get(), and the task throws an ArithmeticException. What does the user see?
Show answer
Nothing about the error. The pool stores the exception in the Future, and because no code calls get(), the exception is never thrown or printed.
Exercise
You call future.get() and catch an ExecutionException named e. How do you find out which exception the task itself threw?
Show answer
Call e.getCause(). It returns the original exception the task threw, such as a NumberFormatException.