Scrivio Interview library
LeadershipAdvanced6 min read · updated 12 November 2026

Teams across your organization handle errors inconsistently: some use exceptions for control flow, some return null, some return result objects. What standard do you set, and how do you roll it out?

I would set one rule that decides by the kind of outcome. Expected business outcomes, such as "not found" or "payment declined", are part of a method's return type, using Optional or a sealed result type; unexpected failures are exceptions; and null is not used for missing results in new code, which returns Optional or empty collections instead. Exceptions are not used for normal control flow. I would make the standard the easy path with shared types and Spring error handling in a common starter, and roll it out to new code first, then to code as it is touched, backed by lint rules and review checklists.

Why this question is asked

Inconsistent error handling costs more than it seems. Every call into another team's code needs reading the implementation to find out whether it returns null, throws, or returns a result, and every wrong guess is a potential NullPointerException or a swallowed failure in production. Interviewers ask this at senior level because the answer is not a single technique but a judgement about which tool fits which situation, plus the ability to change many teams' habits without stopping delivery. In this answer, we will look at what each style is good and bad at, set a rule by kind of outcome, show what the code looks like, and plan the rollout.

1What Each Style Is Good and Bad At

Each of the three styles exists because it solves a real problem, so the standard has to keep what is good about each. Returning null is cheap and familiar, but nothing in the type says a method may return it, so callers forget to check and the failure appears far from its cause. Exceptions are excellent for failures that the immediate caller cannot handle, because they travel up the stack to a handler without every layer having to pass them along. But when they represent ordinary outcomes, the caller is not forced to handle them, the control flow becomes invisible, and filling in stack traces costs real CPU in hot paths.

Result objects make the outcome visible in the type, so the compiler helps callers handle every case. Their weakness is the opposite of exceptions: every layer has to pass them along, which is tedious for failures that nobody in between can do anything about.

Three styles compared
StyleVisible in the type?Caller forced to handle?Best for
Return nullnononothing in new code
Optional / empty collectionyesmostlya lookup that may find nothing
Sealed result typeyesyes, with switchexpected business outcomes with detail
Exceptionno (unchecked)nounexpected failures, bugs, outages

2The Rule: Decide by the Kind of Outcome

The standard asks one question for every way a method can end: is this an outcome the caller is expected to handle as part of normal business, or is it a failure? A card being declined, a coupon having expired, a user not being found by email: these are expected, they happen many times a day, and the caller has a plan for each. They belong in the return type. A database being unreachable, a bug, a corrupted message: the caller has no local plan, and these are exceptions, handled centrally.

From that question the specific rules follow. No null for missing results in new code; a lookup returns Optional, a query returns an empty list. No exceptions for normal control flow, such as ending a loop, signalling a common validation failure in a hot path, or choosing between business branches. Optional is for return values; fields, parameters and collections of Optional are avoided, because they add a wrapper without making anything clearer.

3What the Code Looks Like

For simple absence, Optional is enough. For outcomes that carry detail, a sealed interface with record implementations, available since Java 17, gives a closed set of cases that a switch must cover completely.

Java
Optional<Customer> findByEmail(String email); // may find nothing; never returns null
sealed interface PaymentResult permits Approved, Declined, NeedsVerification {}
record Approved(String transactionId) implements PaymentResult {}
record Declined(String reason) implements PaymentResult {}
record NeedsVerification(URI challengeUrl) implements PaymentResult {}
PaymentResult charge(Order order); // a timeout or bug still throws
String message = switch (payments.charge(order)) {
case Approved a -> "Paid: " + a.transactionId();
case Declined d -> "Declined: " + d.reason();
case NeedsVerification v -> "Verify at " + v.challengeUrl();
}; // adding a fourth case breaks this switch at compile time

Notice what each tool expresses. findByEmail tells the caller in its signature that the customer may be missing. charge tells the caller there are exactly three business outcomes, and the compiler checks that each is handled. Anything else, a payment provider outage or a bug, is still an exception that goes to the shared error handler, which maps it to a problem response and logs it. This is the same separation Java's own libraries are moving towards with Optional and pattern matching.

4Making the Standard the Easy Path

Standards that live only in a document are ignored under deadline pressure. So the platform team ships what teams need in a common starter: a shared @RestControllerAdvice that maps exceptions to RFC 9457 problem details, a small exception hierarchy for failures, and helpers or examples for result types. A team following the standard writes less code than a team inventing its own approach, and that is the strongest argument for adoption.

We also publish a short guide with the decision question, three or four examples from our own domain, and the reasons. Explaining why, for example that null returns caused a given number of NPE incidents last quarter, persuades engineers far better than a list of rules.

5Rolling It Out Without a Big Rewrite

A codebase of many services cannot be converted at once, and should not be. The rollout has stages. First, new code follows the standard from a given date, enforced in code review with a short checklist and by static analysis: for example rules against returning null from methods that return collections, against Optional fields, against catching exceptions only to branch on them, and nullness checking with annotations such as JSpecify and NullAway. Second, code that is touched for other reasons is moved over in the same change, which is the boy scout rule: leave the code a little better than you found it. Third, hot spots identified by incident data, such as modules with frequent NullPointerExceptions, get dedicated work.

Lint rules start as warnings and become errors for new code once the backlog is visible and shrinking. Progress is measured, not assumed: the count of findings per repository and the number of NPE incidents in production show whether the standard is working.

Step throughRolling out the standard
  1. Agree the rule and exampleswith senior engineers from several teams
  2. Ship the common startererror handler, exception types, result examples
  3. New code follows itreview checklist, lint as warnings
  4. Touched code is migratedin the same change, no big rewrite
  5. Lint becomes an erroronce findings are under control
  6. Review incident dataNPEs and swallowed failures per quarter

6 steps in order. Play them, or step through them one at a time.

6Follow-up questions

What about existing APIs that return null?

We do not change their signatures in place, because that would break callers. Instead we add a new method that returns Optional, deprecate the old one, and annotate the old one with @Nullable so tooling warns callers. Over time the callers move and the old method is removed.

Why not use checked exceptions for expected outcomes?

Checked exceptions do force callers to handle them, but they do not work well with lambdas and streams, they encourage catch-and-ignore when people are in a hurry, and they still cost a stack trace. A sealed result type gives the same guarantee through the type system and composes with switch.

How do you get teams to agree?

By involving them in writing it. I would draft the rule with engineers from three or four teams, use examples from our own incidents, and agree to revisit it after a few months. People follow rules they helped shape and that come with tools that make their work easier.

7Conclusion

In this answer, we set a standard based on the kind of outcome: expected business outcomes in return types using Optional or sealed result types, unexpected failures as exceptions handled centrally, no null for missing results in new code, and no exceptions for normal control flow. We made it the easy path through a common starter with shared error handling and types, and rolled it out to new code first, then to touched code, with lint rules, review checklists and incident data to measure progress.

What to say in the interviewAbout 90 seconds aloud

Open with

I would set the standard around one question: is this an expected outcome or a failure? Expected business outcomes, like not found, a declined payment or an expired coupon, go into the return type.

Then, if they want more

For simple absence that is Optional, or an empty collection for queries, and for outcomes with detail it is a sealed interface with records, so a switch has to handle every case and the compiler checks it.

Unexpected failures, like a dependency outage or a bug, are exceptions, and they go to a shared handler that maps them to problem details.

New code does not return null for missing results, and we do not use exceptions for normal control flow like loop exits or common validation in hot code, because that hides the flow and costs stack traces. Optional is only for return values, not fields or parameters.

To get adoption, I would make it the easy path: a common starter with the error handler, a small exception hierarchy and examples, plus a short guide with our own incident examples explaining why.

For the rollout, new code follows it first, with a review checklist and lint rules starting as warnings, then code gets migrated as it is touched, and the lint rules become errors once the backlog is under control. I would track lint findings and NPE incidents to see whether it is working.

About this answer

Last updated
12 November 2026
Versions it names
Java 17
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 "Teams across your organization handle errors inconsistently: some use exceptions for...": https://scrivio-interview-library.netlify.app/java/exceptions/e13e248ac0/