Scrivio Interview library
LeadershipPrincipal6 min read · updated 12 November 2026

Java now has sealed interfaces, records and pattern matching, which make Result-style error handling practical without a library. Over the next decade, how would you guide error handling style across a large codebase that is built on exceptions?

I would keep exceptions as the standard for unexpected failures, because the whole ecosystem integrates with them, and introduce sealed result types for expected outcomes in domain code, starting with new modules rather than rewriting working exception-based code. The language now covers the need, so third-party functional libraries should stay out of public APIs, and every module boundary should use one documented style with explicit conversion at its edges.

Why this question is asked

Error handling is one of those choices that looks local but shapes a codebase for decades. Every method signature, every test and every log line depends on it, and changing it across millions of lines is expensive and risky. Sealed interfaces, records and pattern matching have made Result-style handling practical in plain Java, so senior engineers now have to decide how far to take it. Interviewers ask this to see whether we think in terms of migration paths, ecosystem fit and long-lived conventions rather than personal taste. In this answer, we will look at what changed in the language, where each style fits, how to introduce the new style safely, why the choice of library matters, and how to keep boundaries clean over ten years.

1What Changed in the Language

For most of Java's life, a Result type meant either writing a lot of boilerplate or adopting a library such as Vavr, whose Either and Try types brought their own collection types and conventions. Callers could not get the compiler to check that they handled every case, so the main benefit was weak.

Java 17 made sealed interfaces final, Java 16 records, and Java 21 pattern matching for switch and record patterns. Together they let us write a sealed outcome type in a dozen lines and get exhaustiveness checking for free: a switch over a sealed type must cover every permitted subtype or it does not compile. For the first time, plain Java gives Result-style code the safety that made it attractive in other languages.

2Where Each Style Fits

Exceptions remain the right tool for unexpected failures: a lost database connection, a bug, an invariant violation. Nobody near the failure can do anything useful, so it should travel up to a place that can log it, roll back and return a generic error. Spring transactions, @ControllerAdvice, servlet filters, retry libraries, tracing and test frameworks all integrate with exceptions, and that integration is a large investment we should keep using.

Sealed outcome types fit expected outcomes in domain code: a payment declined, a reservation that conflicts, a rule that rejects an input. These are normal answers that callers must handle, and making them values lets the compiler check every case. A useful test is whether a product owner would describe the outcome as a valid response of the business; if yes, it is an outcome, not an exception.

Choosing a style by the kind of failure
SituationStyleWhy
Card declined, insufficient fundsSealed outcome typeA normal answer the caller must handle
Duplicate order, booking conflictSealed outcome typeBusiness rule; caller shows a message
Database timeout, broken networkExceptionUnexpected; retry, roll back, alert
Null where a value is requiredExceptionA bug; fail fast with a stack trace
Invalid request bodyValidation exception at the web edgeSpring's validation already maps it to 400
The split follows the meaning of the failure, not the layer where it happens.

3Introducing the New Style Without a Rewrite

The tempting plan is a big conversion: replace business exceptions with Result types everywhere. I would refuse it. Rewriting working error paths carries a high risk of changing behaviour, especially transactions, since a returned failure commits where a thrown exception rolled back. It also produces a huge diff that teams cannot review properly, and the benefit for code that is already stable is small.

Instead, the new style enters where it is cheapest and most useful. New modules start with it. Existing modules adopt it when a public operation is being redesigned anyway. Each conversion is a small, reviewable change with tests that cover the transaction outcome as well as the return value. After two or three years, the code that changes often has moved, and the code that never changes stays as it is, which is fine.

Step throughHow the style spreads over the years
  1. Publish the guideOne rule, one worked example, a shared outcome-type pattern
  2. New modules adopt itDomain code returns sealed outcomes from day one
  3. Touched modules convertOnly operations being redesigned anyway
  4. Stable modules stayException-based code that rarely changes is left alone
  5. Review the result yearlyLook at defects and friction, then adjust the guide

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

4Why Libraries Should Stay Out of Public APIs

Before the language features arrived, Vavr's Either or similar types were a reasonable choice. Today I would avoid exposing them in public APIs. A public signature that returns a library's Either forces every consumer to depend on that library, at a version the provider chose, and to learn its conventions. If the library slows down or changes its API in a major release, every consumer pays for the migration.

A sealed outcome type that the module defines itself costs a few lines and depends on nothing but the JDK. It also reads better: TransferOutcome.InsufficientFunds tells the reader far more than Either<Failure, Receipt>. Teams that already use such a library internally can keep it there, as long as it does not leak through module boundaries.

5Keeping One Style per Boundary and Converting at the Edges

The rule that protects the codebase over ten years is one style per module boundary. Inside a module, developers can mix as they need. At the boundary, the public interface either returns outcomes for business results or throws for them, and the module documents which. Where two styles meet, a small adapter converts in one place, so the conversion is visible and tested.

Java
// Exception-based legacy module -> outcome-based domain code
static TransferOutcome fromLegacy(Supplier<Receipt> call) {
try {
return new TransferOutcome.Completed(call.get());
} catch (LegacyInsufficientFundsException e) {
return new TransferOutcome.InsufficientFunds(e.balance());
} // anything else is unexpected and keeps propagating
}
// Outcome-based module -> exception-based caller that cannot change yet
static Receipt orThrow(TransferOutcome outcome) {
return switch (outcome) {
case TransferOutcome.Completed c -> c.receipt();
case TransferOutcome.InsufficientFunds f -> throw new InsufficientFundsException(f.balance());
case TransferOutcome.AccountFrozen f -> throw new AccountFrozenException(f.reason());
};
}

Notice that the first adapter catches only the specific business exception. Anything unexpected keeps propagating as an exception, so rollback and alerting still work. The second adapter's switch is exhaustive, so adding a new outcome later forces the adapter to decide what to throw. I would back the rule with an architecture test that checks public interfaces, and with a short section in each module's documentation.

6Follow-up questions

How do you measure whether the new style is worth it?

I would look at defects in error paths, for example incidents where a business failure was ignored or mapped to the wrong status code, and compare modules that use outcome types with those that do not. Developer feedback in reviews matters too. If the new modules show fewer such defects and teams find them easier to change, the guide is working.

Does this affect performance?

Usually in a good way for expected outcomes. Creating an exception fills in a stack trace, which costs microseconds, and that adds up when a business failure is common, such as validation in a high-traffic endpoint. A small record costs almost nothing. For truly exceptional cases the difference does not matter.

What if teams disagree with the guide?

I would ask for the disagreement as a concrete case, a piece of code that the guide makes worse, and update the guide if the case is convincing. A standard that cannot change becomes something teams work around instead of follow.

7Conclusion

In this answer, we saw that modern Java makes sealed outcome types practical, but exceptions remain the right tool for unexpected failures because the ecosystem is built around them. Over a decade, I would introduce outcome types for expected business results in new and redesigned modules, never through a mass rewrite, keep third-party functional types out of public APIs, and protect the codebase with one style per module boundary and small, tested adapters where styles meet.

What to say in the interviewAbout 80 seconds aloud

Open with

I would treat this as a direction for the next decade, not a migration project. First, exceptions stay the standard for unexpected failures, like timeouts or bugs, because Spring transactions, controller advice, retries, tracing and test tools all integrate with them, and that is a big investment worth keeping.

Then, if they want more

Second, for expected outcomes in domain code, like a declined payment or a booking conflict, we use sealed outcome types, because the compiler now checks that every case is handled. The test I give teams is whether a product owner would call the outcome a valid answer.

Third, the new style enters through new modules and operations that are being redesigned anyway. I would not approve a rewrite of working code, partly because of risk: a returned failure commits a transaction that a thrown exception used to roll back.

Fourth, now that the language covers the need, I would keep libraries like Vavr out of public APIs, so consumers do not inherit a dependency and its upgrade cycle. Finally, each module boundary uses one documented style, and where two styles meet, a small tested adapter converts in one place, catching only the specific business exceptions. I would revisit the guide each year using real defects and team feedback.

About this answer

Last updated
12 November 2026
Versions it names
Java 16, Java 17, Java 21
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 "Java now has sealed interfaces, records and pattern matching, which make Result-style...": https://scrivio-interview-library.netlify.app/java/functional-design/df10019d92/