Interview Q&A · Java · 4 questions in this topic
The Throwable Class and Its Methods — Java Interview Q&A
The root of every throwable and the diagnostic methods interviews expect you to know. Part of Exception Handling · 294 questions in the Java bank.
Throwable sits above both Exception and Error, so it is the only type the JVM can throw. In day-to-day code, catch Exception or a specific subclass, not Throwable: an Error such as OutOfMemoryError signals a condition the application cannot recover from. The diagnostic methods matter because logging frameworks and debugging workflows depend on them — a caught exception without its cause or stack produces an unactionable bug report. When wrapping a failure, always pass the original as the cause.
Why is Throwable important, and what are its key methods?
Answer
Throwable is the root class of everything that can be thrown or caught in Java, with Exception and Error as its two main branches. Only Throwable and its subclasses can follow the throw statement or appear as a catch parameter. Its methods expose diagnostics, chaining, and stack information that handlers and loggers rely on.
Key points
- 1printStackTrace prints the type, message, and full call stack to the error stream.
- 2getMessage returns the detail message and toString adds the class name.
- 3getCause returns the chained cause, or null when no cause was set.
- 4getStackTrace returns the captured frames, useful for logging frameworks.
- 5addSuppressed records secondary failures from try-with-resources cleanup since Java 7.
Example
try { riskyCall(); } catch (Exception e) { System.out.println(e.toString()); System.out.println(e.getMessage()); e.printStackTrace(); }
What is the class hierarchy under Throwable?
Answer
Throwable is the root of everything that can be thrown. Its two direct subclasses are Error, for serious problems a normal application should not try to recover from, and Exception, for conditions a program may handle. RuntimeException extends Exception and marks unchecked failures such as NullPointerException, ClassCastException and IllegalArgumentException; every other Exception subclass is checked and must be declared or caught.
Key points
- 1Throwable → Error and Exception.
- 2Exception → RuntimeException plus checked exceptions.
- 3Only Throwable (and subclasses) may be used in a catch or throw.
- 4getMessage, getCause and printStackTrace live on Throwable.
Why is catching Throwable dangerous?
Answer
catch (Throwable t) also catches Error, including OutOfMemoryError and StackOverflowError. At that point the JVM may be unable to allocate the objects your handler needs, and swallowing the error leaves the process in an unknown state. Catch Throwable only at the outermost boundary of a worker or request handler, where the goal is to log and let that unit fail — never inside normal library code, and never without rethrowing or terminating.
Key points
- 1Throwable includes Error, which applications rarely recover from.
- 2Handling OOM can itself fail to allocate.
- 3A catch-all belongs at a thread or request boundary only.
- 4Always log the full throwable, not just its message.
What is the difference between Exception and Error?
Answer
Exception describes conditions an application can anticipate and handle — a missing file, an invalid argument, a timeout — and is the branch checked exceptions and RuntimeException live under. Error describes problems with the environment or the JVM itself: OutOfMemoryError, StackOverflowError, LinkageError, AssertionError. Applications are not expected to catch Error, because by the time one is thrown there is usually no safe way to continue.
Key points
- 1Exception: application-level, potentially recoverable.
- 2Error: JVM or environment level, generally fatal.
- 3Neither should be caught with an empty handler.
- 4Custom exceptions extend Exception or RuntimeException, not Error.
Learn this topic
Recommended resourcesAffiliate links · may earn us a commission