Java

The Result Pattern

The Result pattern provides an alternative to exceptions for handling expected failures. Instead of throwing, methods return a Result object that can be either Success or Failure. This article covers implementing Result types, when to use them vs exceptions, and functional composition.

📋 At a Glance

AspectDetails
Also Known AsEither, Try, Outcome
Primary BenefitExplicit error handling, functional composition
Trade-offsMore verbose, different mental model
Best ForExpected failures, validation, parsing

🎯 What You'll Learn

  • Result type implementation in Java
  • When to use Result vs exceptions
  • Functional composition with map/flatMap
  • Validation with error accumulation
  • Integration with existing code

Production Story: The Silent Failure Bug

A user import service had a bug that silently skipped invalid users:

JAVA(21 lines)
Code
Loading syntax highlighter...
The fix: Result pattern with error details:
JAVA(14 lines)
Code
Loading syntax highlighter...

Mental Model: Exception vs Result

TEXT(36 lines)
Code
Loading syntax highlighter...

🔬 Deep Dive

Pattern 1: Basic Result Type

JAVA(156 lines)
Code
Loading syntax highlighter...

Pattern 2: Using Result in Business Logic

JAVA(85 lines)
Code
Loading syntax highlighter...

Pattern 3: Controller Integration

JAVA(37 lines)
Code
Loading syntax highlighter...

Pattern 4: Validation with Error Accumulation

JAVA(98 lines)
Code
Loading syntax highlighter...

Pattern 5: Combining Multiple Results

JAVA(62 lines)
Code
Loading syntax highlighter...

Pattern 6: Try - Exception to Result Bridge

JAVA(57 lines)
Code
Loading syntax highlighter...

When to Use Result vs Exceptions

ScenarioUse ResultUse Exception
Validation failures
Parsing/conversion
Expected business failures
Programming errors
Unexpected failures
Infrastructure failures
Cross-boundary errors
Rule of thumb:
  • Result: When failure is part of normal operation (validation, not found, business rule)
  • Exception: When failure is exceptional/unexpected (bugs, system failures)

🐛 Debug This: The Lost Error

A developer reports: "Our Result-based validation always shows success even when there are errors!"

JAVA(29 lines)
Code
Loading syntax highlighter...
Why does validation appear to pass when it should fail?

✅ Solution:
The bug is that the validation result is stored but never used. The code calls validate() but then ignores the returned Result and proceeds to save the user anyway.
JAVA(5 lines)
Code
Loading syntax highlighter...
The fix - chain the validation result:
JAVA(22 lines)
Code
Loading syntax highlighter...
The lesson: Result pattern only works when you chain operations properly. Always use map or flatMap to continue the computation - never ignore the Result!

💻 Exercises

Exercise 1: Implement Basic Result Type

⭐ Difficulty: Easy | ⏱️ Time: 15 minutes

Task: Implement a simple Result type with basic operations.
JAVA(6 lines)
Code
Loading syntax highlighter...
✅ Solution:
JAVA(36 lines)
Code
Loading syntax highlighter...

Exercise 2: Add Transformation Methods

⭐⭐ Difficulty: Medium | ⏱️ Time: 20 minutes

Task: Extend Result with map and flatMap methods.
JAVA(8 lines)
Code
Loading syntax highlighter...
✅ Solution:
JAVA(56 lines)
Code
Loading syntax highlighter...

Exercise 3: Try Helper

⭐⭐ Difficulty: Medium | ⏱️ Time: 15 minutes

Task: Create a Try utility that converts exceptions to Results.
JAVA(7 lines)
Code
Loading syntax highlighter...
✅ Solution:
JAVA(53 lines)
Code
Loading syntax highlighter...

Exercise 4: Validation with Error Accumulation

⭐⭐⭐ Difficulty: Medium-Hard | ⏱️ Time: 25 minutes

Task: Create a validation system that collects all errors instead of failing fast.
JAVA(12 lines)
Code
Loading syntax highlighter...
✅ Solution:
JAVA(103 lines)
Code
Loading syntax highlighter...

Exercise 5: Complete Pipeline with Result

⭐⭐⭐⭐ Difficulty: Hard | ⏱️ Time: 30 minutes

Task: Build a complete data processing pipeline using Result pattern.
JAVA(9 lines)
Code
Loading syntax highlighter...
✅ Solution:
JAVA(123 lines)
Code
Loading syntax highlighter...

🎤 Interview Questions

Q1: What's the advantage of Result over Optional?

Answer:
  • Optional: Only indicates presence/absence, no error info
  • Result: Carries either success value OR error details
JAVA(2 lines)
Code
Loading syntax highlighter...

Q2: When would you NOT use Result pattern?

Answer:
  • Simple CRUD operations where failure is exceptional
  • Code that only throws on programmer error
  • When team isn't familiar with functional patterns
  • Performance-critical paths (Result has some overhead)
  • When exceptions provide better stack traces for debugging

📝 Summary

MethodUse When
map()Transform success value
flatMap()Chain operations that return Result
mapError()Transform error type
getOrElse()Provide default on failure
getOrElseThrow()Convert to exception
onSuccess/onFailureSide effects

📅 Review Schedule for This Article

DayTaskTime
Day 1Review Exception vs Result mental model diagram5 min
Day 3Redo Exercise 2 (Transformation Methods)20 min
Day 7Answer interview questions without looking10 min
Day 14Redo Debug This (Lost Error)15 min
Day 30Identify one service method that could benefit from Result pattern20 min

Next: [Part 16: Global Error Handling & RFC 7807]