Quick revision
The short answer
I would set a split standard: expected business outcomes such as insufficient funds are returned as sealed outcome types, while unexpected failures such as a lost connection or a bug stay as exceptions. Because Spring's rollback and @ControllerAdvice work on exceptions by default, the standard must also say how a returned failure rolls back its transaction and how it becomes an HTTP response, and it must be applied the same way at every module boundary.
What to remember
In this answer, we saw that Spring's transactions, error handling and monitoring are built around exceptions, while sealed outcome types make expected results explicit and force callers to handle them. The standard I would choose splits the two by meaning: outcomes for normal business answers, exceptions for real failures. It must cover the trap where a returned failure commits the transaction, map outcomes to HTTP at the edge, keep one style per module boundary, and be adopted in new code rather than through a rewrite.
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 split standard: expected business outcomes such as insufficient funds are returned as sealed outcome types, while unexpected failures such as a lost connection or a bug stay as exceptions. Because Spring's rollback and @ControllerAdvice work on exceptions by default, the standard must also say how a returned failure rolls back its transaction and how it becomes an HTTP response, and it must be applied the same way at every module boundary.
Why this question is asked
This argument appears in almost every team that adopts modern Java. One group likes Result types because they make failures visible; another points out that Spring was built around exceptions and that half its features assume them. Both are right, and an architect who simply picks a side causes friction for years. Interviewers ask the question to see whether we can turn a style debate into a standard that works with the framework, not against it. In this answer, we will recap how Spring uses exceptions, look at what Result types offer, walk through the trap with transactions, set out the standard I would choose, and show how to roll it out.
1What Spring Expects From Exceptions
Spring wires several cross-cutting features to exceptions. The most important is @Transactional: when a method annotated with it throws a RuntimeException or an Error, the transaction proxy rolls the transaction back; when the method returns normally, it commits. Checked exceptions do not trigger a rollback unless we say so with rollbackFor.
The web layer works the same way. A @ControllerAdvice class with @ExceptionHandler methods turns exceptions into responses, and since Spring Framework 6 the ProblemDetail type gives us a standard error body. Logging, metrics and tracing libraries usually record a failure when they see an exception leave a method. So an exception does three jobs at once in a Spring application: it undoes the database work, it shapes the HTTP error, and it shows up in monitoring.
2What Result Types Offer
A Result or sealed outcome type, such as a TransferOutcome with Completed, InsufficientFunds and AccountFrozen records, makes the possible outcomes part of the method signature. A caller who switches over it must handle every case, and the compiler checks that. With an unchecked InsufficientFundsException, nothing in the signature tells the caller it can happen, and a missing catch only shows up in production.
Expected outcomes are also cheaper and calmer as values. They do not fill stack traces, they do not trip error-rate alarms, and they read like ordinary branches of business logic. Insufficient funds is not a failure of the system; it is one of the normal answers a transfer can give.
3The Trap: A Returned Failure Commits the Transaction
The danger of mixing the two comes from the transaction proxy. It only looks at whether the method threw. If our service has already written something before it discovers the problem and then returns a failure value, the proxy sees a normal return and commits the partial work.
7 messages in order. Play them, or step through them one at a time.
The proxy only rolls back on an exception, so a returned failure keeps any earlier writes.
There are two clean ways to make this safe. The first is to check the business rules before writing anything, so a returned failure has nothing to undo; this is good design anyway. The second, when writes must come first, is to mark the transaction for rollback explicitly with TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(), or to run the work in a TransactionTemplate and decide there. The standard must name one of these, otherwise every team rediscovers the trap on its own.
4The Standard I Would Choose
I would write the standard as one rule with a clear test: if a caller is expected to react to this outcome as part of normal business, return it as a sealed outcome type; if the outcome means something went wrong that the caller cannot sensibly handle, throw. Insufficient funds, a duplicate order or an expired coupon are outcomes. A database timeout, a null where none should be, or a failed call to a downstream service are exceptions.
Then the standard spells out the edges. Inside a domain module, outcome types flow freely. At the web boundary, controllers switch over the outcome and map each case to a response, often a ProblemDetail with a 409 or 422 status, and any exception still goes to the shared @ControllerAdvice. At the transaction boundary, services validate before writing, or explicitly set rollback-only for failure outcomes. For logging, expected outcomes are logged at INFO without a stack trace and counted as business metrics; exceptions are logged at ERROR and drive alerts.
- A failure case appearsFor example, the balance is too low
- Is it part of normal business?Would a product owner describe it as a valid answer?YesReturn a sealed outcome typeNoThrow an exception
- Map at the edgeController switch ->
ProblemDetail; exceptions ->@ControllerAdvice - Check the transactionValidate before writes, or set rollback-only
4 steps in order. Play them, or step through them one at a time.
5Applying It Consistently Across a Large Codebase
The worst outcome is not either style; it is both styles mixed inside the same boundary, so a caller has to catch some failures and switch over others for the same operation. So the rule I would enforce is one style per public module boundary. A module's public service interface either returns outcome types for its business results or throws for them, and the module's README says which.
I would not order a rewrite. Existing exception-based modules keep working; new modules and new public operations follow the standard, and modules are converted when they are being changed for other reasons. A short guide with one worked example, the transfer above, does more than a long policy. An ArchUnit test can stop domain modules from returning outcome types and throwing the matching business exception at the same time, and a review checklist item covers the transaction rule.
6Follow-up questions
Why not make every business error a checked exception instead?
Checked exceptions do make failures visible in the signature, but Spring does not roll back on them by default, lambdas and streams handle them badly, and many teams wrap them in unchecked exceptions anyway, which loses the benefit. Sealed outcome types give the same visibility with exhaustive switches and fewer of those problems.
How do you return a 409 from a controller without an exception?
The controller switches over the outcome and returns a ResponseEntity for each case. For InsufficientFunds it can build a ProblemDetail with status 409, a title and the current balance as a property, so clients see the same error format as the exceptions handled by @ControllerAdvice.
What about a third-party library that throws for a business case?
Catch it at the adapter that calls the library and convert it into the outcome type there. The conversion lives in one place, and the rest of the domain code sees only the standard style.
7Conclusion
In this answer, we saw that Spring's transactions, error handling and monitoring are built around exceptions, while sealed outcome types make expected results explicit and force callers to handle them. The standard I would choose splits the two by meaning: outcomes for normal business answers, exceptions for real failures. It must cover the trap where a returned failure commits the transaction, map outcomes to HTTP at the edge, keep one style per module boundary, and be adopted in new code rather than through a rewrite.
Open with
I would not pick one style for everything; I would split by meaning. If an outcome is a normal business answer, like insufficient funds or a duplicate order, the service returns a sealed outcome type, so the caller has to handle every case and the compiler checks it.
Then, if they want more
If something has actually gone wrong, like a timeout or a bug, it stays an exception. The reason I do not go all in on Result types is that Spring is built around exceptions: @Transactional rolls back only when a runtime exception leaves the method, @ControllerAdvice maps exceptions to responses, and our monitoring counts exceptions as errors.
So the standard has to cover three edges. For transactions, a service checks business rules before writing anything, or explicitly sets rollback-only, because otherwise a returned failure commits partial work. For the API, the controller switches over the outcome and builds a ProblemDetail with the right status, while real exceptions still go through the shared advice.
For logging, expected outcomes are INFO and business metrics, exceptions are ERROR and alerts. Most importantly, each module's public boundary uses one style, documented in its README, so callers never have to both catch and switch for the same operation.
I would roll it out in new modules and when touching old code, not as a rewrite, and back it with a short guide and an ArchUnit check.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Framework 6
- 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.stream, package documentation, java.util.function, package 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 "Teams disagree on whether service-layer code should use exceptions or Result types for...": https://scrivio-interview-library.netlify.app/java/functional-design/994e13314b/