catch evaluates an expression and handles any thrown exception that matches one of its branches.

Syntax

(catch ⟨expr⟩
  ((⟨exception-ctor⟩ ⟨pattern⟩ ...) ⟨handler-body⟩ ...)
  ...
  (_ ⟨fallback-body⟩ ...))

Semantics

  • The first subform is the expression that may throw.
  • Each branch matches either an exception constructor pattern or _ as a catch-all.
  • Patterns are tried in order, including patterns on constructor fields. The first matching branch runs. If none matches, an enclosing handler may handle the exception.
  • All branches must agree on the result type of the catch expression.

Example

(define (crack-safely egg)
  (catch (Ok (crack egg))
    ((DeadlyEgg _) (Err (DeadlyEgg egg)))
    ((UnCracked _) (Err (UnCracked egg)))))