throw signals an exception value to the nearest enclosing
catch or handle
that matches it, or invokes the debugger otherwise.
Syntax
(throw ⟨expr⟩)
Semantics
- The argument’s type must be an instance of the
Exceptionclass, which holds exactly for types defined withdefine-exception.throwhas the typeException :e => :e -> :a, so functions that throw a value of unknown type are polymorphic over exception types. - Control transfers out of the current computation until a matching
catchorhandlebranch is found. - Exception values can be constructed before they are thrown, and a caught exception can be rethrown unchanged.
coalton/result:tryconverts an exception thrown by a function into aResult, andcoalton/result:ok-or-throwdoes the reverse.
Example
(define (crack egg)
(match egg
((Xenomorph)
(throw (DeadlyEgg egg)))
(_ egg)))
;; Inferred type: Exception :e => :e -> :a
(define (rethrow e)
(throw e))