Quick revision
The short answer
I would set a few simple conventions. Every future must be observed, either returned to a caller or ended with a handler that records the error. A failure is logged once, at the boundary that decides what to do with it, not in every stage. CompletionException and ExecutionException are always unwrapped before logging and classifying, and every failure is counted as a metric tagged with its root cause type. I would roll this out through shared helper methods, review checklist items and static checks, starting with the services that page most often.
What to remember
In this answer, we saw that swallowed exceptions, duplicate logs and alerts showing only CompletionException all come from failures being stored in futures. The conventions are that every future is observed, each failure is logged once at the boundary that decides what to do, wrapper exceptions are unwrapped before logging and classifying, and failures are counted by root cause type. Shared helpers make the rules cheap to follow, and checklist items, static checks and a rollout that starts with the noisiest services make them stick.
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.
I would set a few simple conventions. Every future must be observed, either returned to a caller or ended with a handler that records the error. A failure is logged once, at the boundary that decides what to do with it, not in every stage. CompletionException and ExecutionException are always unwrapped before logging and classifying, and every failure is counted as a metric tagged with its root cause type. I would roll this out through shared helper methods, review checklist items and static checks, starting with the services that page most often.
Background to this question
Inconsistent error handling in async code is expensive in a way that is easy to miss: alerts fire without useful information, real failures hide, and on-call engineers spend the first half hour of each incident finding the actual exception. Fixing it is less about one clever technique and more about agreeing on rules and making them easy to follow. In this answer, we will look at why async errors drift into these three bad patterns, define the conventions and the reason for each, show the shared helpers that make them cheap to follow, and plan a rollout that teams will actually adopt.
1Why Async Error Handling Drifts
In synchronous code, an exception travels up the call stack until someone catches it, and an uncaught one at least reaches a framework that logs it. CompletableFuture changes this. A failure becomes a value stored in a future. If nobody observes that future, the failure vanishes silently. If many stages observe it, each one may log it. And when a dependent stage fails, the exception is wrapped in CompletionException, so code that logs the exception directly shows the wrapper rather than the cause.
So the three symptoms in the question, swallowed errors, duplicate logs and alerts showing only CompletionException, all come from the same mechanism. Each team found its own workaround, which is why the code base is inconsistent. A shared convention replaces those workarounds with one way.
| What we see | Why it happens | Convention that fixes it |
|---|---|---|
| Missing work, no error logged | future discarded, failure stored in it | every future is observed |
| Same stack trace 4 times | whenComplete logging in each stage | log once, at the boundary |
Alerts say CompletionException | wrapper logged instead of the cause | unwrap before logging |
| No idea how often it fails | failures only in logs | count failures by root cause type |
2Convention 1: Every Future Is Observed
A future is either returned to the caller, who then owns its outcome, or it ends in a handler that records the failure. A fire-and-forget call like CompletableFuture.runAsync(task) with the result ignored is not allowed unless it ends in such a handler. The reason is simple: a failure stored in an unobserved future can never be seen by anyone, so data loss happens with no trace.
This rule is easy to check. Static analysis tools can flag ignored return values of methods returning CompletableFuture, and many teams already enable that check for other types. We turn it on as an error for our own code.
3Convention 2: Log Once, at the Deciding Boundary
Each failure is logged exactly once, by the code that decides what to do about it: return an error to the user, use a fallback, or give up on a background job. Intermediate stages either recover, or let the failure pass unchanged, or wrap it with added context and rethrow. They do not log. The reason is that one failure should produce one log line and one alert count. With logging in every stage, a single failure produces several stack traces, counts are inflated, and engineers cannot tell one failure from four.
4Conventions 3 and 4: Unwrap, Then Count by Root Cause
Before any code classifies an error or logs it, it unwraps CompletionException and ExecutionException to reach the real cause. Then it records a metric with the root cause type as a tag, so dashboards show, for example, that 80 percent of failures for the pricing dependency are SocketTimeoutException. Logs tell us about one failure; metrics tell us about the trend and drive alerts.
We put both in a shared library so nobody writes them by hand. The helpers below are small, and that is the point: if the right way is one method call, people use it.
Notice that the metric uses a fixed operation name and the exception's class name as tags, never the message or an id, because tags with unbounded values would overload the metrics system.
5Rolling the Conventions Out
Conventions only help if teams adopt them, and a big-bang rewrite rarely succeeds. I would start by writing the four rules on one page with a short example for each, and agree them with the tech leads of the teams that own the most async code, so the rules are theirs too. Next, the shared library with rootCause, observe and a few chain helpers goes into the common platform dependency.
Then we add the rules to the review checklist, with concrete questions such as: is every future either returned to the caller or observed, and does this chain log anywhere except at its end? We enable static analysis for ignored futures, first as a warning so teams can see their count, then as an error for new code. Finally, we fix existing code where it hurts most: services whose alerts most often show CompletionException are the first candidates, because there the change visibly shortens incidents.
We measure progress with numbers people care about: the share of alerts whose root cause type is known, the count of ignored-future warnings per service, and duplicate log lines per failure. When those numbers improve, the conventions sell themselves.
- Agree four rules with tech leadsone page, one example each
- Ship helpers in the platform library
rootCause, observe, chain helpers - Add review checklist itemsobserved? logged once? unwrapped?
- Static check for ignored futureswarning first, then error for new code
- Fix the noisiest services firstwhere alerts show only
CompletionException - Track known-cause alert shareprogress visible to every team
6 steps in order. Play them, or step through them one at a time.
6Follow-up questions
Why not log in every stage just to be safe?
Because it is not safer, only noisier. One failure becomes several log entries and alert counts, and on-call engineers cannot tell whether there was one problem or four. Logging once at the deciding boundary, with context added by wrapping along the way, gives one complete entry per failure.
What if a team uses Reactor instead of CompletableFuture?
The same four rules apply in different words: every publisher is subscribed or returned, errors are logged once at the edge with doOnError only there, wrapped exceptions are unwrapped, and failures are counted. The shared library can offer an equivalent helper for Reactor.
How do you handle teams that disagree with a rule?
Ask for the concrete case. Often the disagreement reveals a missing helper or an unclear rule, for example a background job that has no boundary. Adjust the rule or add the helper, and keep the decision documented, rather than allowing silent exceptions.
7Conclusion
In this answer, we saw that swallowed exceptions, duplicate logs and alerts showing only CompletionException all come from failures being stored in futures. The conventions are that every future is observed, each failure is logged once at the boundary that decides what to do, wrapper exceptions are unwrapped before logging and classifying, and failures are counted by root cause type. Shared helpers make the rules cheap to follow, and checklist items, static checks and a rollout that starts with the noisiest services make them stick.
Open with
All three symptoms come from the same thing: in CompletableFuture a failure is a value stored in a future. If nobody observes the future, it vanishes; if every stage observes it, it is logged many times; and because dependent stages wrap it in CompletionException, logs show the wrapper.
Then, if they want more
So I would define four conventions. First, every future is observed: it is either returned to the caller or ends with a handler that records the failure, and fire-and-forget without a handler is not allowed.
Second, a failure is logged once, at the boundary that decides what to do with it, like the controller or the job runner; intermediate stages recover, pass the failure on, or wrap it with context, but do not log. Third, we always unwrap CompletionException and ExecutionException before logging and classifying.
Fourth, every failure increments a metric tagged with the operation and the root cause type, so we can alert on trends.
To roll it out, I would agree the rules with the tech leads, put small helpers like rootCause and observe in the shared platform library, add checklist items to code review, and turn on a static check for ignored futures, as a warning first.
Then I would fix the services whose alerts show CompletionException most often, and track the share of alerts with a known root cause to show progress.
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
- java.util.concurrent, package documentation, JLS chapter 17, Threads and Locks
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 "Async code across your teams handles errors inconsistently: some chains swallow...": https://scrivio-interview-library.netlify.app/java/completable-future/e008becf35/