MediumExperience level: 3-5 years · 8-15 years2 min read

Task Exceptions in Executors — Interview Questions

Reviewed by Gurusankar M.

Asked inAmazonMicrosoftDeloitteOracle
#executor#exception handling#future#executionexception#submit vs execute
Report issue

Short Answer

It depends on how you submitted. With execute(Runnable), an uncaught exception propagates to the thread's UncaughtExceptionHandler and typically prints a stack trace. With submit(...), the exception is captured inside the returned Future and stays silent until you call Future.get(), which rethrows it wrapped in an ExecutionException. So submit() 'swallows' failures unless you actually inspect the Future — a very common source of invisible errors.

On this page

Concept Made Simple

Coffee Chat Question

“Where do exceptions go when a task fails in an executor, and why does submit() swallow them?”

Remember It Faster

Mind Map Answer

execute(Runnable)
Exception → UncaughtExceptionHandler
submit(...)
Exception stored in the Future (silent)
future.get()
Rethrows as ExecutionException
Result
Unchecked get() → errors vanish

The trap: you fire off submit(task), never look at the Future (fire-and-forget), and the task throws. Nothing is logged, no handler runs — the failure is buried in a Future nobody reads. Switching to execute or always inspecting the Future surfaces it.

For pools, set a ThreadFactory with an UncaughtExceptionHandler (and/or wrap task bodies in try/catch with logging) so failures are never silent, regardless of submit vs execute.

Key takeaway: submit hides exceptions in the Future; execute routes them to the uncaught handler. Either read every Future's result or log inside the task — never fire-and-forget with submit.

Learn by Doing

Hands-on Keyboard

java
ExecutorService pool = Executors.newFixedThreadPool(2);

// submit(): exception is hidden until you call get()
Future<?> f = pool.submit(() -> { throw new IllegalStateException("boom"); });
try {
    f.get();                       // rethrows wrapped
} catch (ExecutionException e) {
    System.out.println("caught: " + e.getCause());  // the real cause
} catch (InterruptedException e) {
    Thread.currentThread().interrupt();
}
Output
caught: java.lang.IllegalStateException: boom

Think Beyond the Expected

What If?

A pooled task clearly throws, but nothing appears in the logs — why?

You almost certainly used submit() and never called get() on the returned Future. submit() catches the throwable and stores it in the Future as the task's outcome instead of propagating it, so no UncaughtExceptionHandler fires and nothing is logged. The exception only re-surfaces (wrapped in ExecutionException) when someone calls Future.get(). Fixes: inspect every Future, wrap the task body in try/catch with logging, or use execute() so uncaught exceptions reach the thread's handler.

Real World

This is behind countless 'the background job just stopped working and we had no idea' incidents — scheduled tasks, async event handlers, and worker pools where results are ignored. Teams standardise on logging inside task bodies and installing an UncaughtExceptionHandler so no failure is ever silent, especially for fire-and-forget work.

Interviewer's Expectation

Keywords they're listening for:

  • execute → uncaught handler
  • submit → exception in Future
  • get() rethrows ExecutionException
  • fire-and-forget submit hides errors
  • install UncaughtExceptionHandler / log in task

Common Mistakes

  • Fire-and-forget submit() without reading the Future
  • Assuming submit exceptions reach the uncaught handler
  • Not logging inside task bodies

Best Practices

  • Inspect every Future or log within the task
  • Install an UncaughtExceptionHandler via a ThreadFactory
  • Prefer CompletableFuture with explicit error handling for async flows

Follow-up Questions

  1. How do you set an UncaughtExceptionHandler for pool threads?
  2. How does CompletableFuture surface exceptions differently?
  3. What happens to a scheduled task that throws?

Continue Learning with AI

Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly — AI is your learning companion, not a shortcut.

Plain-language foundations

I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner.

Topic: Executors (Advanced Java)
Interview question: "Where do exceptions go when a task fails in an executor, and why does submit() swallow them?"

Please:
1. Explain the core idea in simple, plain language, using an everyday analogy.
2. Define any technical terms you use.
3. Walk through one small, concrete example.
4. Finish with a single sentence I can easily remember.

Keep the tone friendly and assume I'm new to this topic.
Open inChatGPTGeminiClaude

Was this answer helpful?

Affiliate links

Equipment for a comfortable, productive setup.

As an Amazon Associate I earn from qualifying purchases.