Scrivio Interview library
DebuggingFoundations6 min read · updated 12 November 2026

A task fails but nothing is logged:

Java
executor.submit(() -> {
process(order);
});

where process throws a RuntimeException. Explain why and give two fixes.

Nothing is logged because submit() wraps the task in a FutureTask, which catches the RuntimeException and stores it inside the returned Future. The exception only appears if someone calls future.get(), which throws ExecutionException; since the code throws the Future away, the failure disappears. We fix it by keeping the Future and calling get(), by catching and logging inside the task, by using execute() so the thread's uncaught exception handler sees it, or by overriding ThreadPoolExecutor.afterExecute() to inspect completed futures.

Why this question is asked

This bug is easy to write and painful to find. Orders stop being processed, no error appears in the logs, and the thread pool looks healthy. Interviewers like it because it tests whether we know what executors do with exceptions, which is not obvious from the API. In this answer, we will reproduce the silent failure, follow the exception to where it is stored, compare with execute(), and then go through the fixes and when each fits.

1Reproducing the Silent Failure

Here is the code from the question in a small program. process() throws for an invalid order.

Java
ExecutorService executor = Executors.newFixedThreadPool(4);
Order order = new Order(-1);
executor.submit(() -> { process(order); }); // process throws IllegalStateException
// Console output: nothing at all
// The pool thread is fine and goes on to the next task

Notice that the lambda body is a block with a void call, so it matches submit(Runnable), and the returned Future<?> is ignored. No stack trace, no log line. If we had used execute() instead, we would at least see a stack trace on standard error.

2Where the Exception Goes

submit() does not hand our lambda to a thread directly. It wraps it in a FutureTask, an object that is both a Runnable for the pool thread and a Future for the caller. FutureTask.run() calls our code inside a try/catch that catches every Throwable. When process() throws, FutureTask stores the exception in its outcome field, marks itself as completed exceptionally, and returns normally.

So from the pool thread's point of view, the task finished without a problem. The thread never sees the exception, its uncaught exception handler is never called, and the exception sits inside a Future that nobody holds. Once the Future is garbage collected, the error is gone forever. The exception only becomes visible when someone calls get(), which throws ExecutionException with the original exception as its cause.

Java
Future<?> f = executor.submit(() -> { process(order); });
try {
f.get();
} catch (ExecutionException e) {
System.out.println(e.getCause()); // java.lang.IllegalStateException: invalid order -1
}
Step throughsubmit() versus execute() when the task throws
CallerPool threadFutureTaskUncaught handler
submit(task) wraps it
run()
catch exception, store in Future
returns normally: nothing to report
execute(task): no wrapper
task throws out of run()
uncaughtException(thread, e)
prints the stack trace to System.err

8 messages in order. Play them, or step through them one at a time.

With submit() the exception stops inside the FutureTask; with execute() it reaches the thread.

3Why execute() Behaves Differently

execute() passes our Runnable straight to the worker thread. When the task throws, ThreadPoolExecutor first calls afterExecute() with the exception, then lets it escape the worker's run loop. The worker thread dies, the pool replaces it with a new one, and the JVM calls the thread's uncaught exception handler. If no handler is set, the default one from the ThreadGroup prints Exception in thread "pool-1-thread-1" and the stack trace to System.err.

That is better than silence, but it is still not great: System.err may not reach our log aggregation, and a dying thread on every failure costs a thread creation. So execute() is a reasonable choice only together with a proper uncaught exception handler on the pool's ThreadFactory.

4Fix 1: Keep the Future and Handle the Error

If the caller cares about the outcome, it should keep the Future and call get(), or better, use CompletableFuture so the error handling is attached to the work itself.

Java
CompletableFuture
.runAsync(() -> process(order), executor)
.exceptionally(ex -> { // runs when process() throws
log.error("Order {} failed", order.id(), ex);
return null;
});

This keeps the call non-blocking while making sure every failure has a handler. The trap remains that someone may forget the exceptionally() stage, so for fire-and-forget tasks the next fix is often safer.

5Fix 2: Catch and Log Inside the Task

The simplest fix that cannot be forgotten by callers is to make the task responsible for its own failures. A small wrapper can apply it to every task we submit.

Java
static Runnable logged(String name, Runnable task) {
return () -> {
try {
task.run();
} catch (RuntimeException e) {
log.error("Task {} failed", name, e); // logged with our logger, with context
throw e; // still visible to the Future, if anyone asks
}
};
}
executor.submit(logged("process-order-" + order.id(), () -> process(order)));

Rethrowing keeps the exception in the Future too, so a caller who does call get() still sees it. The log line now has the order id, which a generic handler could never add.

6Fix 3: Override afterExecute() for the Whole Pool

When we own the pool, we can inspect every finished task in one place. For a task given to submit(), the Throwable argument of afterExecute() is null, because FutureTask already swallowed it, so we look inside the future ourselves. This is the approach shown in the ThreadPoolExecutor documentation.

Java
class LoggingPool extends ThreadPoolExecutor {
LoggingPool(int n) { super(n, n, 0, TimeUnit.SECONDS, new LinkedBlockingQueue<>()); }
@Override
protected void afterExecute(Runnable r, Throwable t) {
super.afterExecute(r, t);
if (t == null && r instanceof Future<?> f && f.isDone() && !f.isCancelled()) {
try {
f.get(); // already done, so it does not block
} catch (ExecutionException e) {
t = e.getCause(); // the IllegalStateException from process()
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
if (t != null) log.error("Task failed", t);
}
}

Since Java 19 the Future interface also has state() and exceptionNow(), which make this check shorter (check your JDK). The pool-level approach catches failures that individual developers forget, at the cost of less context in the log line, so in practice we combine it with Fix 2 for important tasks.

7Follow-up questions

Does a CompletableFuture swallow exceptions the same way?

Yes. runAsync() and supplyAsync() store the exception in the CompletableFuture, and it is only seen by a later stage such as exceptionally(), handle() or whenComplete(), or by join() or get(). If the chain has no such stage and nobody joins, the failure is just as silent.

Does a ScheduledExecutorService behave the same?

Yes, and worse. A periodic task scheduled with scheduleAtFixedRate() that throws is stored in its ScheduledFuture and all future runs are cancelled, silently. Periodic tasks should always catch and log inside the task.

Would setting a default uncaught exception handler fix the submit() case?

No. The handler is only called for exceptions that escape a thread's run method. With submit(), FutureTask catches the exception first, so the thread never sees it and the handler is never invoked.

8Conclusion

In this answer, we saw that submit() wraps the task in a FutureTask that catches the exception and stores it in the Future, so it is only visible through get() as an ExecutionException. execute() lets the exception reach the thread and its uncaught exception handler, which prints it by default. We fixed the silent failure by handling the Future or a CompletableFuture stage, by catching and logging inside the task, and by overriding afterExecute() to inspect finished futures for the whole pool.

What to say in the interviewAbout 90 seconds aloud

Open with

The reason nothing is logged is that submit doesn't run my lambda directly. It wraps it in a FutureTask, and FutureTask's run method catches any exception from the task and stores it in the Future it returned to me.

Then, if they want more

From the worker thread's point of view the task completed normally, so the uncaught exception handler never runs. The only way to see the exception is to call get on that Future, which throws ExecutionException with the original exception as the cause, and this code throws the Future away.

If I used execute instead, the exception would escape the worker, the pool would replace the thread, and the default uncaught exception handler would print the stack trace to standard error. For fixes, the first option is to keep the Future and handle the error, or use CompletableFuture.runAsync with an exceptionally stage that logs it.

The second, which I'd use for fire-and-forget work, is to catch and log inside the task, ideally with a wrapper that adds context like the order id and rethrows.

And if I own the pool, I can override ThreadPoolExecutor.afterExecute: for submitted tasks the throwable argument is null, so I check whether the runnable is a done Future and call get to pull out the cause, then log it. That gives a safety net for every task in the pool.

About this answer

Last updated
12 November 2026
Versions it names
Java 19
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "A task fails but nothing is logged:": https://scrivio-interview-library.netlify.app/java/threads/ef133d8a20/