Overview

8 Functional error handling

This chapter explains functional error handling as an alternative to exception-driven programming. Instead of using throw and try-catch to interrupt control flow, functions express possible failure in their return types and carry errors as ordinary values. That shift makes outcomes explicit, keeps computations composable, and helps separate business-level failures from technical ones.

The central abstraction is Either, a two-case type with Left for failure and Right for success. The chapter shows how Either generalizes Option by allowing error details to be preserved, and how core combinators such as Map, Bind, and Match support chaining computations that may fail. It also highlights an important limitation: filtering with Where is awkward for Either because a failed predicate does not naturally supply a left value.

Through examples like recruitment workflows and a future transfer API, the chapter demonstrates how to model validation and persistence as explicit outcomes. It distinguishes business validation errors from unexpected technical exceptions, then refines the design with specialized types such as Validation for rule violations and Exceptional for wrapped exceptions. Finally, it shows how to translate these functional results at application boundaries, such as HTTP controllers, while preserving clear, maintainable logic inside the core of the application.

Either represents one of two possible outcomes. It indicates that a computation could potentially result in a Left or a Right. The image depicts an example in which the left branch is taken.
Which shop would you prefer as a client?
If everything goes according to plan . . .
Chaining Either-returning functions
With Option and Either, Match is used to leave the abstraction.
The outer layer of an application consists of adapters.
In functional error handling, errors are treated as part of the returned payload.

Summary

  • Use Either to represent the result of an operation with two different possible outcomes, typically success or failure. An Either can be in one of two states:
    • Left indicates failure and contains error information for an unsuccessful operation.
    • Right indicates success and contains the result of a successful operation.
  • Interact with Either using the equivalents of the core functions already seen with Option:
    • Map and Bind apply the mapped/bound function if the Either is in the Right state; otherwise, they just pass along the Left value.
    • Match allows you to handle the Right and Left cases differently.
    • Where is not readily applicable; if you want to filter out certain Right values according to a predicate, use Bind instead, providing a function that yields a suitable Left value if the predicate fails.
  • Either is particularly useful for combining several validation functions with Bind or, more generally, for combining several operations, each of which can fail.
  • Because Either is rather abstract and because of the syntactic overhead of its two generic arguments, in practice, it’s better to use a particularized version of Either, such as Validation and Exceptional.
  • When working with functors and monads, prefer using functions that stay within the abstraction, like Map and Bind. Use the downward-crossing Match function as little or as late as possible.

FAQ

What problem does functional error handling solve compared with exceptions?Functional error handling avoids disrupting normal control flow with throw and try-catch. Instead, failure is represented in the return type, so errors become regular values that the caller can handle explicitly.
When should you use Option instead of Either for error handling?Use Option when there is only one kind of failure to report and no extra error details are needed, such as parsing a value or looking up an item that may be missing.
What does Either<L, R> represent?Either<L, R> represents one of two possible outcomes: Left(L) for failure or Right(R) for success. In error handling, Left usually holds error information and Right holds the successful result.
Why is Right associated with success and Left with failure in this chapter?By convention, Right is used for the happy path and Left for errors. A helpful mnemonic is: Right = all right and Left = something wrong.
How do Map and Bind work with Either?They apply functions only when the value is in the Right state. If the value is already Left, the error is passed along unchanged and later computations are skipped.
Why can’t Where be implemented for Either in the same way as for Option?Where needs a “zero” or fallback value when a predicate fails. With Either<L, R>, there is no default value of type L, so you can’t automatically construct a Left from just a failed predicate.
What is the difference between business validation and technical errors?Business validation errors represent expected rule violations, such as an invalid transfer date or bad BIC. Technical errors are unexpected faults, such as database failures or configuration problems, and should usually be treated differently.
Why does the chapter recommend using Validation for business errors and Exceptional for technical errors?Validation is suited to business-rule failures, while Exceptional models unexpected technical problems. Keeping them separate makes workflows clearer and lets you handle each kind of failure appropriately.
How can an API expose functional outcomes to client applications?An API can translate an internal Either or Option into an HTTP response, such as 404 for None or 400/500 for failures, or it can return a DTO that serializes both success and error information in the response body.
What are the main advantages of specialized types like Validation and Exceptional over plain Either?They use more descriptive names, reduce generic noise in signatures, and fix the failure type to a meaningful domain choice. This can make code more readable and better express the intent of the workflow.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Functional Programming in C#, Second Edition Functional Programming in C#, Second Edition ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Functional Programming in C#, Second Edition Functional Programming in C#, Second Edition ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Functional Programming in C#, Second Edition Functional Programming in C#, Second Edition ebook for free