liveBook · Manning

raiseError

As explained in Functional Programming in Scala, Second Edition

raiseError is an effect operation that constructs a computation representing a specified error.

raiseError constructs an effectful computation representing a specified Throwable rather than throwing it immediately. It belongs to MonadThrow, which extends Monad with explicit error raising and handling. section 15.3.2

The operation in MonadThrow

trait MonadThrow[F[_]] extends Monad[F]: def raiseError[A](t: Throwable): F[A] The result type remains F[A], so the failure is represented inside the effect and can later participate in attempt or handleErrorWith.

Stream and Pull add raiseError constructors so computations can explicitly fail during incremental processing. The operation also supports combinators such as onComplete, which can run a final stream and then re-raise the original error. section 15.3.2

How Functional Programming in Scala, Second Edition covers it

How deep it goes
Defines the typeclass operation and shows its use in stream error propagation, without detailing concrete effect implementations.
Examples
Uses a MonadThrow signature and an onComplete definition that appends the final stream before re-raising the source error.
What it assumes
Assumes familiarity with Monad and introduces error capabilities through attempt, handleErrorWith, and raiseError.
What you can do after
Apply it via MonadThrow to construct typed effectful failures and preserve them through stream cleanup.
How it connects
Connects MonadThrow with Monad, attempt, handleErrorWith, Pull, Stream, and resource finalization.

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.