Throwable Class Methods in Java
Fast recall
Quick revision
Core answer
Throwable provides the shared machinery every exception inherits: `getMessage()` returns the description, `getCause()` returns the wrapped underlying exception (enabling exception chaining), `printStackTrace()` dumps the call stack to stderr, `getStackTrace()` returns the frames as an array for logging frameworks, and `addSuppressed()`/`getSuppressed()` carry extra exceptions (used by try-with-resources).Whether you know Throwable is more than a marker — it carries the diagnostic methods, especially getCause for exception chaining
Not knowing getCause exists, so wrapping an exception without passing the original and losing the root cause in production
Stress that preserving the cause chain is what makes a production stack trace debuggable, and contrast printStackTrace with logging via getStackTrace
Interview-ready guide
Interview answer
Throwable is the root of the exception hierarchy, but it's also where all the diagnostic plumbing lives, so every exception inherits these methods.
The most basic one is getMessage, which returns the human-readable description you passed into the constructor. The one I'd really emphasise, because freshers often miss it, is getCause. That returns the underlying exception that triggered this one, and it's the basis of exception chaining.
Say your data layer throws a S Q L exception but your service layer wants to throw its own domain exception — you pass the S Q L exception in as the cause, so later anyone can call getCause and recover the original error, including the database error code. If you don't preserve that cause, you've thrown away the information you'll need to debug the problem in production.
Then there's printStackTrace, which everyone knows from tutorials — it dumps the full call stack to standard error. That's fine for a quick local check, but in production you shouldn't use it because it bypasses your logging framework. Instead you use getStackTrace, which gives you the stack frames as an array of StackTraceElement objects that a logger can format properly.
There's also addSuppressed and getSuppressed, which carry secondary exceptions; that's how try-with-resources records a failure from closing a resource without losing the primary exception. The big mistake to avoid is wrapping an exception and forgetting to pass the original as the cause, because then the chain is broken and your logs show the wrapper but not the actual reason.
When I debug, I walk the cause chain by calling getCause repeatedly, because the real culprit is often a few layers down.
Throwable Class Methods in Java
Direct Answer
Throwable provides the shared machinery every exception inherits: getMessage() returns the description, getCause() returns the wrapped underlying exception (enabling exception chaining), printStackTrace() dumps the call stack to stderr, getStackTrace() returns the frames as an array for logging frameworks, and addSuppressed()/getSuppressed() carry extra exceptions (used by try-with-resources).
Common Mistakes & Pitfalls
- Wrapping without the cause —
throw new XException(msg)instead ofthrow new XException(msg, e)discards the root cause. - Using
printStackTrace()in production — it writes tostderr, bypassing your log framework; log the exception object instead. - Never walking
getCause()— the real diagnostic info is often several layers down the chain. - Confusing
getMessage()withtoString()—toString()includes the class name;getMessage()is just the text.
Check your understanding
Answer these without looking back at the article.
- What is exception chaining and why is it useful?
- What is the difference between getMessage() and toString() on an exception?
- When would you use getStackTrace() instead of printStackTrace()?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.