Rethrowing Exceptions in Java
Fast recall
Quick revision
Core answer
throw e; when its current type and context already fit. Translate it with throw new DomainException("useful context", e); when crossing a real abstraction boundary, preserving the original as the cause. Cleanup belongs in try-with-resources or finally; do not catch merely to log if a higher boundary will log the same failure.Whether you know how to rethrow while preserving the original cause via exception chaining
Rethrowing a new exception without passing the original as the cause, losing the root stack trace
Mention Java 7 'precise rethrow' lets you catch Exception and rethrow specific subtypes when the throws clause allows
Interview-ready guide
Interview answer
I rethrow the same exception with throw e when its type already fits and this catch has a real reason, such as a local recovery attempt that failed. If the abstraction changes, I wrap it in a meaningful exception and pass the original as the cause.
A catch that only rethrows is normally unnecessary. Cleanup belongs in try-with-resources or finally, and I do not automatically log before rethrowing when an outer boundary will log the same incident.
Rethrowing Exceptions in Java
Direct Answer
Rethrow the same caught object with throw e; when its current type and context already fit. Translate it with throw new DomainException("useful context", e); when crossing a real abstraction boundary, preserving the original as the cause. Cleanup belongs in try-with-resources or finally; do not catch merely to log if a higher boundary will log the same failure.
Common Mistakes & Pitfalls
- Wrapping without the cause —
new X(msg)instead ofnew X(msg, e)drops the root. - Log-and-rethrow — logging then rethrowing the same exception causes duplicate log entries up the stack.
- Swallowing — catching and not rethrowing when you can't actually handle it.
- Changing the type for no reason — only wrap when the new type adds meaning to your layer.
Check your understanding
Answer these without looking back at the article.
- Does rethrowing change the stack trace?
- What is the difference between throw e and throw new Exception(e)?
- When should you log before rethrowing?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.