Difference between using Throwable and Exception in a try catch
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In Java, error handling is an essential part of writing robust applications. Java provides a robust mechanism to handle exceptions and errors through its exception handling model. Central to this model are the concepts of Exceptions and Throwables, which are often used in a try-catch block. Understanding the difference between handling a Throwable and an Exception can greatly impact how well an application manages failures and unexpected issues.
Understanding Throwable and Exception
In Java’s hierarchy, Throwable is the superclass of all errors and exceptions. Direct subclasses of Throwable are Exception and Error. Here's how the hierarchy is structured:
- Throwable
- Exception: Meant for conditions that a reasonable application might want to catch.
- Error: Intended for serious errors from which a program is not expected to recover.
While both Exception and Error derive from Throwable, their handling and use case typically differ.
When to Catch Throwable
Catching Throwable is generally not recommended except in specific circumstances where you must catch and handle all possible problems no matter their nature, including system errors. Such situations include:
- When running critical server applications where you need to prevent the server from crashing, catching
Throwablemight make sense. This allows an application to remain running by catching unforeseen errors likeVirtualMachineError. - For top-level error handlers that log every issue that occurred at any point in the execution.
Here is an example of catching Throwable:
When to Catch Exception
Catching Exception is more common and usually recommended. Handling Exception covers all the exceptions that might be thrown during normal operation of the program, including runtime exceptions (unchecked exceptions) like NullPointerException, and other checked exceptions like IOException.
Example:
Key Differences in Use
Here is a comparison table summarizing the key differences:
| Aspect | Throwable | Exception |
| Scope of errors | Includes all errors and exceptions | Includes all recoverable exceptions |
| Recovery | Not typically used for recovery | Used for expected recovery scenarios |
| Use cases | Critical systems, global error handlers | Application level error handling |
| Impact on application | Can prevent application termination | Helps in graceful error recovery |
| Best practice recommendation | Not recommended unless necessary | Recommended for handling expected conditions |
Conclusion
While both Throwable and Exception can be caught in try-catch blocks, their use should be carefully considered based on the type of errors your application needs to handle. Catching Throwable is generally for critical situations where you need a failsafe against all types of errors, potentially at the risk of masking serious issues. On the other hand, catching Exception is more aligned with handling expected and specific issues that the application can recover from. In most situations, it is advisable to catch specific exceptions your code is likely to throw, as this promotes clearer, more maintainable error handling.
Additional Tips
- Always try to catch the most specific exception types first, before falling back to more general types.
- Consider the scope and impact of any error caught at a level too high, as it could mask significant problems.
- Use finally blocks or try-with-resources statements to ensure that resources are closed properly regardless of whether an exception is thrown or not.
By understanding these concepts and the principles of Java’s exception handling, programmers can write more reliable and fault-tolerant applications.

