Learn Java
import com.learnjava.exceptionhandling.ExceptionChaining;

Exception Chaining in Java

@Concept

Exception chaining preserves an original exception as the cause of a new one, rather than losing that original information when translating a low-level failure into a higher-level, more meaningful exception — throw new ServiceException("Failed to load user", originalException); wraps originalException as the new exception's cause, keeping its full stack trace and message intact and accessible. Without chaining, catching a low-level exception and throwing a different, more meaningful one in its place would silently discard exactly the detail that's most useful for genuinely diagnosing the root problem later.

getCause() retrieves that wrapped original exception from the new one, and a printed stack trace for a chained exception shows both exceptions together, clearly labeled, with the original cause's trace appearing right underneath the wrapping exception's own. No diagnostic detail from the original failure is actually lost, even though the code that catches it deals with the new, more meaningful exception type instead.

@HowItWorks
1

The problem chaining solves

Translating a low-level exception into a more meaningful one can lose the original failure's detail if that original isn't explicitly preserved.

2

Passing the original as a cause

Most exception constructors accept a Throwable as a second argument specifically for this purpose.

3

getCause()

Retrieves the originally wrapped exception from a chained one, when it's actually needed for diagnosis.

4

Stack traces show the full chain

Both the wrapping exception's own trace and the original cause's trace appear together, clearly connected.

5

When chaining matters most

Anywhere a lower-level technical exception, like from a database or file, gets translated into a higher-level, more meaningful one for callers.

@Example
public class ChainingDemo {
static void loadConfig() throws Exception {
try {
throw new NumberFormatException("bad port value");
} catch (NumberFormatException e) {
throw new Exception("Failed to load configuration", e);
}
}
 
public static void main(String[] args) {
try {
loadConfig();
} catch (Exception e) {
System.out.println("Caught: " + e.getMessage());
System.out.println("Caused by: " + e.getCause().getMessage());
}
}
}
Output
Caught: Failed to load configuration
Caused by: bad port value
1loadConfig catches the original, low-level NumberFormatException internally.
2It then throws a new, more meaningful Exception, passing the original one as its second constructor argument to chain it as the cause.
3main's catch block catches that new, higher-level exception and reads its own message first.
4e.getCause() retrieves the original wrapped exception, and its message confirms the original, specific failure is still fully accessible.
@UseCases
Translating a low-level technical exception into a more meaningful, domain-specific one without losing diagnostic detail
Reading a chained stack trace to find the true, original root cause underneath a higher-level exception
Wrapping a checked exception a method doesn't want to declare into an unchecked one, while preserving it as the cause
Debugging a production failure by following a chain of causes back to its actual origin

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.