liveBook · Manning

Asynchronous exception

As explained in Learn Haskell by Example

AsyncException is a Haskell exception type whose constructors represent asynchronous runtime events such as stack overflow, thread termination, and user interruption.

An asynchronous exception is raised by code outside the affected thread to change that thread’s control flow. Haskell represents these runtime events with the AsyncException type. section 14.4.1 section 14.5

AsyncException has four constructors:

  • StackOverflow is raised when a thread exceeds its stack-size limit. section 14.4.1
  • HeapOverflow is raised when the program’s heap reaches its limit. section 14.4.1
  • ThreadKilled stops a thread, usually after another thread throws the exception to it. section 14.4.1
  • UserInterrupt represents cancellation by the user, such as pressing Ctrl-C. section 14.4.1

Catching UserInterrupt is useful when an operation must undo partial work. A file copy can leave a partially copied file behind, so the handler removes that file before rethrowing the interruption. section 14.4.1

Cleaning up an interrupted copy

Use catchJust with a predicate that selects UserInterrupt, run the copy action, remove the destination in the handler, and rethrow UserInterrupt.

How Learn Haskell by Example covers it

How deep it goes
Defines four constructors and demonstrates selective UserInterrupt handling, but does not develop broader asynchronous-exception internals.
Examples
Uses Ctrl-C during metadata-preserving file copying and removes the partially copied destination.
What you can do after
Apply it via catchJust to select UserInterrupt, clean up partial work, and rethrow the interruption.
How it connects
Connects asynchronous exceptions with synchronous exceptions, threads, throwTo, catchJust, tryJust, and exception-driven control flow.

Also explained in

Compare how 2 books explain asynchronous 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.