Java Throwable vs Exception in try-catch: which to catch
Codemia Knowledge Hub ·
Throwable catches everything: both Exception (recoverable problems) and Error (fatal JVM-level problems like OutOfMemoryError). Exception catches only the recoverable subset. In nearly all application code, catch Exception, or better, specific exception types. Reserve catch (Throwable) for top-level error handlers, shutdown hooks and thread pool tasks where you need to make sure a failure is never silently lost.
The Java exception hierarchy
A catch clause matches the named type and all of its subclasses. Catching Throwable matches the whole tree. Catching Exception matches everything except the Error branch.
Catching Exception in application code
Exception covers the problems application code is expected to handle: checked exceptions (I/O failures, network errors, parse errors) and unchecked runtime exceptions (null references, illegal arguments, array bounds violations).
This catches the expected exception first (IOException), then uses a broader Exception catch as a safety net. It is the recommended approach for most application code.
When catching Throwable is justified
Catching Throwable includes Error subtypes, which represent conditions the JVM considers unrecoverable:
| Error Type | Meaning | Should you catch it? |
OutOfMemoryError | JVM heap is exhausted | Almost never (state is corrupted) |
StackOverflowError | Call stack exceeded | Rarely (indicates infinite recursion) |
NoClassDefFoundError | Required class missing at runtime | Sometimes (classpath issues in plugins) |
VirtualMachineError | JVM itself is failing | Never (JVM is dying) |
AssertionError | Assertion failed | Rarely (indicates logic bugs) |
LinkageError | Class loading/linking failure | Sometimes (in classloader-heavy systems) |
The legitimate reasons are narrow.
1. Tasks submitted to a thread pool
When a task is passed to submit(), anything it throws, including an OutOfMemoryError, is stored in the returned Future. If nobody calls Future.get(), the failure is invisible. Catching Throwable inside the task makes sure it is at least logged.
2. Shutdown hooks
3. Plugin or classloader isolation boundaries
Side-by-side comparison
| Aspect | catch (Throwable t) | catch (Exception e) |
| Catches checked exceptions | Yes | Yes |
| Catches runtime exceptions | Yes | Yes |
Catches Error subtypes | Yes | No |
| Typical use case | Framework-level error handling | Application-level error handling |
| Recovery expectation | Low (log and fail) | High (handle and continue) |
| Risk of masking fatal errors | High | Low |
| Best practice recommendation | Only at thread/process boundaries | Preferred for business logic |
Prefer catching specific exceptions
Both catch (Throwable) and catch (Exception) are broad. Usually you should catch the most specific type you expect.
Multi-catch (Java 7+)
Multi-catch, introduced in Java 7, lets you handle several specific exceptions the same way without catching a broad type:
This beats catch (Exception e) because it documents which exceptions you expect and does not accidentally catch ones you did not anticipate.
What catching OutOfMemoryError actually does
Catching an OutOfMemoryError does not free memory. The application's state is likely corrupted: collections may be partially populated, transactions half-committed, and locks held indefinitely. The thread keeps running, but the application is unreliable.
The safe pattern is to catch it only at the outermost level (for example in a thread pool's error handler), log it, and shut down the application or reject the current request.
Mistakes with broad catches
- Catching
Throwablein application code and swallowing it. This hides fatal JVM problems likeOutOfMemoryError, and the application continues in a corrupt state, producing wrong results or deadlocking. Always re-throw or terminate after catchingThrowable. - Catching
Exceptionand silently returning null. Log even recoverable exceptions. A silent null pushes the failure downstream, where it surfaces as aNullPointerExceptionwith no hint of the root cause. - Catching
Errorto "recover" from it.StackOverflowErrorandOutOfMemoryErrormean fundamental resources are exhausted, and recovery code is likely to trigger the same error. Catch them only for logging and shutdown. - Using
catch (Exception e)when you know the specific exceptions. Broad catches swallow unexpected exceptions and hide bugs. Catch the narrowest type possible. - Ordering catch clauses wrongly. Catch clauses are evaluated in order, so catching
Exceptionbefore a subtype makes the specific block unreachable. The compiler flags this, but it is an easy mistake during refactoring. - Dropping the original exception when re-throwing. Always chain:
throw new ServiceException("message", e). Losing the cause makes debugging nearly impossible.
Related reading
- Difference between Valid and Validated in Spring
- Difference between volatile and synchronized in Java
- Difference between WAIT and BLOCKED thread states
- Difference between "wait()" vs "sleep()" in Java
- Difference in System. exit(0) , System.exit(-1), System.exit(1 ) in Java
- Difference of HashMap in alt-rt.jar and rt.jar?
- differences between 2 JUnit Assert classes
- Differences between Ant and Maven
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.