liveBook · Manning

Exception

As explained in Learn Haskell by Example

An exception type is a Haskell type with an instance of the Exception type class whose values represent abnormal computational conditions.

An exception is a Haskell value whose type has an Exception instance and can represent an abnormal computational condition. Exceptions change control flow by transferring execution to a caller or handler, making them useful when interaction with files, networks, or other processes can fail. section 14.3.3 section 14.5

The core exception workflow includes:

  • Define a custom exception type and give it Show and Exception instances; DeriveAnyClass can create the empty Exception instance. section 14.4
  • Throw exceptions with throw or throwIO; throw has a polymorphic result, while throwIO produces an IO action. section 14.3.3
  • Catch a specific exception type with catch rather than broadly catching SomeException, which may also intercept user interruptions or asynchronous control flow. section 14.4
  • Use try when the failure should become an Either value, or catches when handling multiple exception types. section 14.4 section 14.5

A typed failure handler

With the DeriveAnyClass extension enabled, define data SomeFailure = SomeFailure String deriving (Show, Exception). Throw SomeFailure "Whoops" and handle it with catch using a SomeFailure pattern; unrelated exceptions from error or undefined remain uncaught. section 14.4

Listing 14.4 A safe listing function that encodes exceptions in the return value
import Control.Exception

...

safeListFilesAndDirectories ::
  FilePath -> IO (Either IOException ([FilePath], [FilePath]))
safeListFilesAndDirectories = try .
 listFilesAndDirectories             #1

A safe file-listing function converts IOException failures into the return value IO (Either IOException ([FilePath], [FilePath])), allowing callers to handle failure explicitly. section 14.4

How Learn Haskell by Example covers it

How deep it goes
Defines exception values, throwing, typed catching, broad-catching risks, and Either-based recovery while deferring detailed exception categories.
Examples
Uses SomeFailure, error, undefined, typed catch, and safe file listing with IOException encoded in Either.
What it assumes
Assumes basic Haskell types, IO, monadic computations, and type classes while contrasting exceptions with MonadFail failures.
What you can do after
Apply it via Control.Exception to define, throw, catch, or encode typed failures for IO operations.
How it connects
Connects exceptions with IO, MonadFail, SomeException, catch, try, catches, bracket, and asynchronous exceptions.

Also explained in

Compare how 25 books explain exception →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Haskell by Example. The section links point to the parts of the book it draws on.