Java
UncaughtExceptionHandler
ExecutorService
Multithreading
Concurrency

Why is UncaughtExceptionHandler not called by ExecutorService?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

In Java, the `UncaughtExceptionHandler` is a mechanism designed to catch and handle exceptions that are not caught within a thread. While using the `ExecutorService` to manage and execute threads, many developers assume that `UncaughtExceptionHandler` will handle any uncaught exceptions thrown from tasks submitted to the service. However, this is not the case, and understanding why this happens requires an exploration of how the `ExecutorService` operates.

ExecutorService and Task Execution

How ExecutorService Works

The `ExecutorService` in Java is part of the `java.util.concurrent` package and is used to manage and execute threads. It abstracts thread management details by handling thread creation, execution, and termination:

  1. Submitting Tasks: Tasks can be submitted to an `ExecutorService` using methods such as `submit()` and `execute()`.
  2. Execution: The service uses an internal thread pool to execute tasks concurrently.
  3. Task Completion: Once a task is completed, whether successfully or due to an exception, the thread is returned to the pool.

Why UncaughtExceptionHandler Is Not Invoked

The key reason the `UncaughtExceptionHandler` is not invoked for tasks submitted to an `ExecutorService` lies in how exceptions are handled:

  1. Runnable vs. Callable: When using `ExecutorService`, tasks can be either `Runnable` or `Callable`. Both these interfaces are designed to represent tasks carrying a `void` and `V` return type, respectively. This implies no direct mechanism within them triggers any form of exception handling.
  2. Future.get(): When you submit a task using `submit()`, it returns a `Future` object. You must explicitly call `Future.get()` to retrieve the result or catch exceptions like `ExecutionException`, which wraps any thrown exceptions.
  3. Exception Handling within Threads: The `UncaughtExceptionHandler` is only invoked when a thread terminates due to an uncaught exception. However, in `ExecutorService`, exceptions within tasks are captured and encapsulated by the framework, preventing them from propagating up to the thread level where `UncaughtExceptionHandler` operates.

Example Code

Below is a simple demonstration where an uncaught exception within an `ExecutorService` managed task won't trigger the `UncaughtExceptionHandler`:


Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.