What is the difference between a Throwable and an Exception in Java?
Throwable is the root class of Java's exception-handling hierarchy. Both Exception and Error inherit from Throwable. Exception represents conditions that an application can potentially handle and recover from, whereas Error represents serious system-level problems that applications typically should not attempt to handle.
Java exception class hierarchy
Key Points
Throwable is the superclass of both Exception and Error.
Exception represents recoverable conditions that can be handled using try-catch blocks.
Error represents critical issues such as JVM failures or memory shortages.
Most application-level problems are handled through Exception classes.
Errors generally indicate problems outside the control of the application.
Exception vs Throwable
Throwable
Root class of the exception hierarchy
Parent of Exception and Error
Can be thrown using the throw keyword
Represents all abnormal conditions
Exception
Subclass of Throwable
Represents recoverable application-level issues
Can be handled using try-catch
Includes both checked and unchecked exceptions
Example
If a file is missing during execution, Java throws a FileNotFoundException, which is an Exception and can be handled. However, if the JVM runs out of memory, it throws an OutOfMemoryError, which is an Error and usually cannot be recovered from safely.
Catching Throwable or Error in normal code, which also catches OutOfMemoryError and other problems the program cannot recover from.
Thinking all exceptions are checked. RuntimeException and its subclasses are unchecked.
Follow-up Questions
Can you create your own Throwable subclass? Technically yes, but you should extend Exception or RuntimeException instead.
Is Error checked or unchecked? Unchecked: the compiler does not force you to handle it.
Interview Tip
A concise interview answer is
Throwable is the top-level class in Java's error-handling hierarchy. Exception is a subclass of Throwable used for recoverable conditions that applications can handle, while Error is another subclass that represents serious system-level problems that are generally not meant to be handled by application code.