Quick revision
The short answer
The snippet has three problems. It catches every Exception, including programming errors that should fail fast; it reports through printStackTrace, which writes to stderr and bypasses the logging system with its levels and request context; and it swallows the failure, so the caller believes the order was processed. The fix is to catch only exceptions we can handle, log them through the logger with the exception object, and then rethrow or return an explicit failure, restoring the interrupt flag if InterruptedException can be caught.
What to remember
In this answer, we reviewed try { process(order); } catch (Exception e) { e.printStackTrace(); } and found three problems: the catch is too broad and hides programming errors, printStackTrace bypasses the logging system and its context, and the failure is swallowed so callers think the order was processed. We rewrote it to catch only exceptions with a handling plan, log through the logger with the exception, return an explicit result or rethrow with the cause, and restore the interrupt flag when InterruptedException is caught.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The snippet has three problems. It catches every Exception, including programming errors that should fail fast; it reports through printStackTrace, which writes to stderr and bypasses the logging system with its levels and request context; and it swallows the failure, so the caller believes the order was processed. The fix is to catch only exceptions we can handle, log them through the logger with the exception object, and then rethrow or return an explicit failure, restoring the interrupt flag if InterruptedException can be caught.
Why this question is asked
This four-line pattern is everywhere: IDEs generate it, tutorials use it, and it survives into production more often than anyone would like. Each of its three parts hides failures in a different way, and together they produce the worst kind of bug, one where data is wrong and nothing was reported. Interviewers ask us to review it because it shows whether we think about what happens after an exception, not just about making the code compile. In this answer, we will look at each problem in turn with the reason it matters, then rewrite the code properly and see how the rewrite behaves.
1Problem 1: catch (Exception e) Catches Too Much
Exception is the parent of nearly everything that can go wrong in Java code: checked exceptions such as IOException, but also every RuntimeException, including NullPointerException, IllegalArgumentException, ClassCastException and IndexOutOfBoundsException. Those last ones are usually bugs, not conditions to handle. A broad catch treats a missing null check in our own code the same way as a timeout from a payment provider.
Programming errors should fail fast, meaning the request fails loudly and visibly at the point of the bug, so it gets noticed and fixed. When a broad catch absorbs them, the bug can live for months. The right approach is to catch only the exception types we have a plan for, such as PaymentDeclinedException or a timeout we can retry, and let the rest propagate to the top-level handler of the request or job.
2Problem 2: printStackTrace Bypasses Logging
e.printStackTrace() writes the trace to System.err, the process's standard error stream. That output does not go through SLF4J, Logback or Log4j 2, so it has no timestamp in the standard format, no log level, no logger name, and none of the context we put in the MDC, the mapped diagnostic context that holds values like the request ID, user ID and trace ID for the current thread.
In practice that means three things. A log platform that parses JSON log lines receives multi-line plain text it cannot parse, often split into one event per stack frame. Nobody can filter it by level or service or link it to the request that caused it. And alerting on errors does not see it at all. In some deployments stderr is not collected at all, and the trace disappears.
| Aspect | e.printStackTrace() | log.error("...", orderId, e) |
|---|---|---|
| Destination | System.err, plain text | configured appenders and log platform |
| Level and logger name | none | ERROR, com.acme.OrderService |
| Request ID, trace ID | missing | included from the MDC |
| Counted by error alerts | no | yes |
| Format | multi-line text, often split | one structured event with the trace |
3Problem 3: The Failure Is Swallowed
After printing, the code simply continues. The method returns normally, and whoever called it, a controller, a message listener, a batch loop, concludes that the order was processed. The HTTP response is 200, the Kafka message offset is committed, the batch marks the row as done. The order was not processed, and the system has now recorded that it was.
This is the most damaging of the three problems, because it turns a failure that could have been retried into silent data loss. A message listener that throws would have the message redelivered; one that swallows the exception loses it.
4Rewriting the Code
Here is a version that handles what it can and reports the rest honestly. It assumes process can fail with a declined payment, which is an expected outcome, and with a timeout from a dependency.
Each catch now has a plan: a declined payment becomes an explicit result the caller must look at, and a timeout becomes an exception that a retry mechanism understands, with the original as its cause. Both log through the logger with the exception object as the last argument, so the trace, causes and MDC context are included. Notice that everything else, including the NullPointerException from a bug, is not caught here at all. It reaches the framework's top-level handler, which logs it once and fails the request.
5The InterruptedException Detail
If the code inside the try can throw InterruptedException, for example because process waits on a future or sleeps between retries, a broad catch (Exception e) also catches the interrupt and clears it. That breaks cancellation and executor shutdown. So if we catch it, we restore the flag and stop.
6Follow-up questions
Is catch (Exception e) ever acceptable?
Yes, at the top level of a unit of work, such as a scheduled job's run method, a message listener or a thread's main loop, where the job is to log the failure once and keep the process alive for the next unit. Even there, the failure must be reported properly: logged with the exception, counted in metrics, and the message or job marked as failed.
Why not log and rethrow in every layer?
Because the same exception then appears in the logs three or four times, which makes incidents harder to read. Either handle it and log it, or rethrow it with added context and let the top-level handler log it once.
What should the code do if the caller cannot handle a result type?
Then the method should throw. The point is that the caller must learn about the failure in a way it cannot ignore. A result type works when callers are written to inspect it; an exception works when they are not.
7Conclusion
In this answer, we reviewed try { process(order); } catch (Exception e) { e.printStackTrace(); } and found three problems: the catch is too broad and hides programming errors, printStackTrace bypasses the logging system and its context, and the failure is swallowed so callers think the order was processed. We rewrote it to catch only exceptions with a handling plan, log through the logger with the exception, return an explicit result or rethrow with the cause, and restore the interrupt flag when InterruptedException is caught.
Open with
I see three problems. First, catching Exception is too broad. It also catches NullPointerException, IllegalArgumentException and other programming errors that should fail fast, so bugs get hidden.
Then, if they want more
I would catch only the exceptions I have a plan for, like a declined payment or a timeout, and let the rest go to the top-level handler. Second, printStackTrace writes to stderr.
It bypasses the logging framework, so there is no level, no logger name, no request or trace ID from the MDC, it often breaks structured log parsing, and error alerts never see it. I would use the logger and pass the exception as the last argument so the full trace and causes are printed.
Third, and worst, the failure is swallowed. The method returns normally, so the caller thinks the order was processed, a 200 goes back or the Kafka offset gets committed, and the order is silently lost.
After logging, the code should either rethrow, ideally wrapped with context and the original as the cause, or return an explicit failure result the caller has to check. And if anything inside can throw InterruptedException, a broad catch clears the interrupt, so I would catch it separately, call Thread.currentThread().interrupt() and stop.
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.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
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 "Review this code and list the problems:": https://scrivio-interview-library.netlify.app/java/exceptions/492d40870e/