4 Handling errors without exceptions

This chapter covers

  • Discussing the disadvantages of exceptions
  • Introducing the Option data type
  • Introducing the Either data type
  • Introducing the Try data type

We noted briefly in chapter 1 that throwing exceptions is a side effect. If exceptions aren’t used in functional code, what is used instead? In this chapter, we’ll learn the basic principles for raising and handling errors functionally. The big idea is that we can represent failures and exceptions with ordinary values, and we can write higher-order functions that abstract out common patterns of error handling and recovery. The functional solution of returning errors as values is safer and retains referential transparency, and through the use of higher-order ...

Get Functional Programming in Scala, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.