4 Handling errors without exceptions

This chapter covers

  • Pitfalls of throwing exceptions
  • Understanding why exceptions break referential transparency
  • Handling exceptional cases: a functional approach
  • Using Option to encode success and ignore failure
  • Applying Either to encode successes and failures

We noted briefly in chapter 1 that throwing an exception is a side effect and an undesired behavior. But why do we consider throwing exceptions bad? Why is it not the desired effect? The answer has much to do with a loss of control. At the point that an exception is thrown, control is delegated away from the program, and the exception is propagated up the call stack. This loss of control means one of two things: the program will be terminated because ...

Get Functional Programming in Kotlin 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.