Scrivio Interview library
DebuggingIntermediate6 min read · updated 12 November 2026

A service logs throw new ServiceException("Failed to load customer") inside a catch block, and support cannot find why loading failed. What is wrong and how do you review for it?

The code creates a new ServiceException inside the catch block without passing the caught exception as its cause, so the original exception's type, message and stack trace are thrown away and the log shows only "Failed to load customer". The fix is new ServiceException("Failed to load customer " + id, e), or initCause(e) if no cause constructor exists, and the review rule is that every rethrow in a catch block keeps the cause or says why not, backed by static analysis.

Why this question is asked

This is one of the most common reasons an on-call engineer cannot explain a failure. The error is logged, the stack trace points neatly at our own throw statement, and the real reason, a timeout, a constraint violation, a parse error, is gone. Interviewers ask it because it is easy to spot once we know what to look for, and because the answer shows whether we think about the people who will debug our code later. In this answer, we will reproduce the problem, see exactly what is lost and why, fix it in two ways, and then set up a review rule and tooling so it stops happening.

1Reproducing the Problem

Here is a typical version of the code. The repository call fails, and the catch block translates the failure into a service-level exception.

Java
Customer load(long id) {
try {
return customerRepository.findById(id).orElseThrow();
} catch (DataAccessException e) {
throw new ServiceException("Failed to load customer"); // e is never used
}
}
// What the log shows:
// com.acme.ServiceException: Failed to load customer
// at com.acme.CustomerService.load(CustomerService.java:24)
// at com.acme.CustomerController.get(CustomerController.java:31)
// ...
// (no Caused by: section at all)

The stack trace is accurate, but it only describes where ServiceException was created. It does not say which customer failed, and it does not say why. The DataAccessException, which might have said "Connection is not available, request timed out after 30000ms", existed for a moment in the variable e and was then dropped.

2Why the Original Exception Is Lost

An exception records its stack trace when it is constructed, not when it is thrown. So a new exception created in a catch block gets a fresh stack trace starting at that line. The only link back to the original failure is the cause field, which is set when we pass the original exception to the constructor or call initCause.

When the cause is null, nothing connects the two exceptions. The original object becomes garbage as soon as the catch block ends. Notice that the compiler does not warn about this: an unused catch parameter is perfectly legal Java. That is why this bug survives code review so often; it looks like reasonable error translation.

What the log contains
InformationWithout the causeWith the cause
Our messageFailed to load customerFailed to load customer 42
Where we rethrewyesyes
Original exception typelostCaused by: DataAccessResourceFailureException
Original messagelostpool timeout after 30000ms
Original stack tracelostprinted under Caused by:
The cause is the only link between the two exceptions.

3Fix 1: Pass the Cause and Add Context

The fix is one argument. We also add the customer ID to the message, because the second piece of information support needed was which customer failed.

Java
} catch (DataAccessException e) {
throw new ServiceException("Failed to load customer " + id, e);
}
// Log now:
// com.acme.ServiceException: Failed to load customer 42
// at com.acme.CustomerService.load(CustomerService.java:24)
// ...
// Caused by: org.springframework.dao.DataAccessResourceFailureException: Unable to acquire JDBC Connection
// ...
// Caused by: java.sql.SQLTransientConnectionException: HikariPool-1 - Connection is not available, request timed out after 30000ms

Now the log tells the whole story from top to bottom: the business operation that failed, the customer, and the root cause at the bottom of the chain. This requires ServiceException to have a constructor taking a cause, which every well-designed exception should have.

4Fix 2: initCause When There Is No Cause Constructor

Sometimes we must throw an exception class we do not control, and it has only a message constructor. Throwable.initCause sets the cause after construction.

Java
} catch (DataAccessException e) {
LegacyServiceException ex = new LegacyServiceException("Failed to load customer " + id);
ex.initCause(e); // returns ex, so throw (LegacyServiceException) ex.initCause(e) also works
throw ex;
}

initCause can be called at most once, and only if the cause was not already set by a constructor; otherwise it throws IllegalStateException. So it is a fallback, not a style to prefer. If we own the exception class, adding a cause constructor is the better fix.

5Turning It Into a Review Rule and a Build Check

For code review, the rule is short: every throw inside a catch block either passes the caught exception as the cause, or has a comment explaining why not. Legitimate reasons exist but are rare, for example when the original exception carries sensitive data that must not reach a log, in which case we still log a sanitized version or a reference.

People miss things in review, so we add a tool. Sonar has a rule that flags exception handlers which neither log nor pass on the caught exception (it describes this as preserving the original exception; check the rule key in your version). Error Prone and PMD have similar checks, and a Semgrep pattern can match a throw new inside a catch whose arguments do not include the catch variable. We run the rule across the codebase in report mode first to see how many cases exist, fix them, and then make it fail the build.

Finally, the same review should look at log statements in catch blocks. A log.error("Failed: " + e.getMessage()) has the same effect as a missing cause: the trace and causes never reach the log. Passing e as the last argument of the logger fixes it.

6Follow-up questions

Is it ever right to drop the cause on purpose?

Occasionally, at a trust boundary. For example, a public API should not send internal exception details to clients. But the right approach there is to log the full exception with a correlation ID on the server and return a generic error to the client, not to drop the cause inside the service. Inside our own code, the cause should always be kept.

What if we catch an exception and handle it fully?

Then no exception is rethrown and there is no cause to keep, but we should still log it if the situation is unusual, with the exception object, so the information is not lost. An empty catch block, or one that only returns a default value silently, hides failures in the same way.

Does wrapping exceptions at every layer cause very long traces?

It can if every layer wraps. Wrapping is useful when it adds context, such as the operation and the ID, or translates to a type the caller understands. If a layer has nothing to add, it is better to let the exception pass through unchanged than to wrap it once more.

7Conclusion

In this answer, we saw that creating a new exception in a catch block without passing the caught one as its cause loses the original type, message and stack trace, because exceptions record their own trace at construction and the cause is the only link back. We fixed it by passing e as the cause, adding the ID to the message, or calling initCause when no cause constructor exists. We then made it a review rule and a static analysis check so it stays fixed.

What to say in the interviewAbout 90 seconds aloud

Open with

The problem is that the catch block creates a new ServiceException and never passes the caught exception to it. A new exception gets a fresh stack trace from the line where it is created, and the only link to the original failure is the cause.

Then, if they want more

Without it, the original exception's type, message and stack trace are thrown away, so the log shows Failed to load customer pointing at our own throw, and nothing about the real reason, like a connection pool timeout.

The fix is to pass e as the cause and add context, so new ServiceException with Failed to load customer plus the id, and e. Then the log shows our message followed by Caused by sections down to the root cause.

If the exception class has no cause constructor, I can call initCause once on it before throwing, but I would rather add the constructor. For review, my rule is that every throw inside a catch either passes the caught exception or has a comment explaining why.

I back that with static analysis, for example the Sonar rule about preserving the original exception or a Semgrep pattern, first in report mode to clean up existing cases, then failing the build. I also check catch blocks that log only getMessage, because they lose the trace in the same way.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "A service logs throw new ServiceException("Failed to load customer") inside a catch...": https://scrivio-interview-library.netlify.app/java/exceptions/aed6f0a7a2/