Interview Q&A · Java · 4 questions in this topic

The Throwable Class and Its Methods — Java Interview Q&A

All Java topics

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

  1. 1printStackTrace prints the type, message, and full call stack to the error stream.
  2. 2getMessage returns the detail message and toString adds the class name.
  3. 3getCause returns the chained cause, or null when no cause was set.
  4. 4getStackTrace returns the captured frames, useful for logging frameworks.
  5. 5addSuppressed records secondary failures from try-with-resources cleanup since Java 7.

Example

Example.javajava
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

  1. 1Throwable → Error and Exception.
  2. 2Exception → RuntimeException plus checked exceptions.
  3. 3Only Throwable (and subclasses) may be used in a catch or throw.
  4. 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

  1. 1Throwable includes Error, which applications rarely recover from.
  2. 2Handling OOM can itself fail to allocate.
  3. 3A catch-all belongs at a thread or request boundary only.
  4. 4Always log the full throwable, not just its message.

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits