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.
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.
Code Example:
try {
String str = null;
System.out.println(str.length());
} catch (Exception e) {
System.out.println("Exception handled");
}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."