liveBook · Manning

Exception

As explained in Functional Programming in Scala, Second Edition

An exception is an abnormal runtime condition represented by a control-flow mechanism that can interrupt normal computation and be handled separately.

An exception is an abnormal runtime condition that interrupts normal computation and can be handled separately. In functional programming, throwing one is treated as a side effect because it breaks referential transparency and makes meaning depend on evaluation context. section 4.1

Throwing and catching

A function can throw inside a try expression and recover with a catch clause. The same thrown expression may instead escape when evaluated elsewhere, showing that its meaning depends on the surrounding try block. section 4.1

Exceptions create two central problems:

  • Break local reasoning by making expression meaning context-dependent. section 4.1
  • Hide possible failure from function types, allowing unhandled errors to survive until runtime. section 4.1 section 4.6
  • Replace recoverable exceptions with ordinary values such as Option, Either, Try, or Validated, whose higher-order functions compose failure handling. section 4.5 section 4.6

How Functional Programming in Scala, Second Edition covers it

How deep it goes
Explains throwing, catching, referential-transparency failures, context dependence, and type unsafety before deferring implementation to error ADTs.
Examples
Uses Scala functions that throw Exception values inside try/catch expressions and return either a failure or recovered integer.
What it assumes
Assumes familiarity with referential transparency and substitution while demonstrating exception behavior through concrete Scala functions.
What you can do after
Evaluate when exceptions obscure reasoning and model recoverable failures with typed values and composable operations.
How it connects
Connects exceptions with referential transparency, Option, Either, Try, Validated, higher-order functions, and error handling.

Also explained in

Compare how 25 books explain exception →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Functional Programming in Scala, Second Edition. The section links point to the parts of the book it draws on.