Chained Exceptions in Stack Traces
Fast recall
Quick revision
Core answer
cause of a higher-level exception, usually with new HigherLevelException(message, cause). This lets the outer layer add useful context without losing the original type, message, and frames. Read the complete chain; the deepest recorded cause is not always the most actionable explanation.Interview-ready guide
Interview answer
A chained exception is a higher-level throwable that keeps a lower-level throwable in its cause.
For example, new OrderLoadException("Cannot load order 42", sqlException) adds useful operation context without losing the SQL exception's type, message, and frames. The printed trace shows the wrapper first and then Caused by: blocks. I read the full chain: the outer layers explain what the system was doing, while the deeper cause explains the lowest recorded failure.
The deepest cause is not automatically the actionable defect; incorrect input or configuration may have produced it. A good wrapper adds context and passes the original cause. Re-wrapping at every layer with a generic message only adds noise.
Suppressed: exceptions, often from resource cleanup, are related diagnostics but are not links in the cause chain.
Chained Exceptions in Stack Traces
Direct Answer
A chained exception keeps a lower-level throwable as the cause of a higher-level exception, usually with new HigherLevelException(message, cause). This lets the outer layer add useful context without losing the original type, message, and frames. Read the complete chain; the deepest recorded cause is not always the most actionable explanation.
Deep Dive & Architecture
Exception chaining preserves two kinds of information. The outer exception names the operation or domain failure, while its cause retains the lower-level technical failure.
For example, OrderLoadException("Cannot load order 42", sqlException) tells the caller what operation failed and lets diagnostics inspect the database exception. Passing only sqlException.getMessage() would discard its type, cause chain, and stack frames.
A useful wrapper adds context and preserves the cause. Avoid wrapping at every layer with no new information. Also distinguish causes from suppressed exceptions, which commonly record secondary failures during resource cleanup.
Common Mistakes & Pitfalls
- Discarding the cause when creating a wrapper.
- Adding a wrapper with no useful context at every layer.
- Assuming the deepest cause alone names the fix.
- Exposing low-level exceptions directly across an API boundary without translation.
- Ignoring suppressed cleanup failures.
Check your understanding
Answer these without looking back at the article.
- How do you read a Java stack trace to find the source of an error?
- What does 'Caused by' in a Java stack trace mean and how do you use it?
- How do you read a NullPointerException stack trace in Java?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.