Why Custom Exceptions Need a Cause Constructor
Fast recall
Quick revision
Core answer
(String message, Throwable cause) constructor makes correct exception translation easy: it records the original failure as the cause, so getCause() and formatted stack traces retain the chain. Throwable.initCause() can set a cause in some cases, so the constructor is not the only mechanism, but it is the normal and safest API for custom exceptions.Whether you understand that the cause constructor enables exception chaining and preserves the root stack trace
Defining a custom exception with only a message constructor, then losing the original cause when wrapping
Connect it to the Caused by: section in logs and to clean layer translation that stays debuggable
Interview-ready guide
Interview answer
A cause constructor lets code translate an exception without losing the original failure. The custom exception adds a message meaningful to its layer and passes the original Throwable to super(message, cause). getCause() and the printed trace then show the chain.
initCause() can attach a cause in supported cases, so a constructor is not the only mechanism, but it is the clearest and safest normal API. I never replace the cause with only e.getMessage(), and I keep sensitive diagnostics out of public error responses.
Why Custom Exceptions Need a Cause Constructor
Direct Answer
A (String message, Throwable cause) constructor makes correct exception translation easy: it records the original failure as the cause, so getCause() and formatted stack traces retain the chain. Throwable.initCause() can set a cause in some cases, so the constructor is not the only mechanism, but it is the normal and safest API for custom exceptions.
Common Mistakes & Pitfalls
- Only a
(String)constructor — there's no way to attach the cause later. - Wrapping with
new X(msg)even when a cause constructor exists — pass the cause. - Logging the cause separately then dropping it — chain it instead so the trace is one unit.
- Assuming the JVM links causes automatically — you must pass it explicitly.
Check your understanding
Answer these without looking back at the article.
- What is getCause() and what does it return?
- How does exception chaining appear in a stack trace?
- What is 'Caused by' in a Java stack trace?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.