Arc's simplest mechanism for non-linear control flow is
throw. This allows execution to jump back to the
catch expression, for example to exit a loop early.
arc> (catch (each x '(2 4 6 9 10 12) (prn "Examining " x) (if (odd x) (throw x)))) Examining 2 Examining 4 Examining 6 Examining 9 9
Arc also provides access to MzScheme's exception mechanism, which provides a way to trap errors or raise error.
ccc function is equivalent to Scheme's
call-with-current-continuation (often abbreviated as
call/cc). Continuations are extremely powerful; for details
see Teach Yourself Scheme in Fixnum Days, Chapter 20 of "On Lisp",
Call with Current Continuation Patterns, or Advanced Scheme Techniques. In MzScheme, continuations are relatively expensive if the call stack is deep since capturing a continuation copies the stack.
point name [body ...]
Creates a 'throw' function called
>(+ 10 (point throw (+ 20 (throw 3) 30))) 13
>(+ 10 (catch (+ 20 (throw 3) 30))) 13
Packages up the current continuation into an 'escape procedure' and passes it to the procedure. Equivalent to Scheme's
>(ccc (fn (ep) (ep "bailout value") 42)) "bailout value"
protect during-procedure after-procedure
Uses Scheme's dynamic-wind to ensure that
>(protect (fn () (/ 1 0)) (fn () (prn "after"))) after Error: /: division by zero
after during-body [after-body ...]
Returns the message associated with an exception.
>(on-err (fn (ex) (details ex)) (err "boo")) Error: boo
err string ...
Raises an exception with the given text.
>(err "Failure" 42) Error: Failure 42
on-err err-proc proc
Executes proc. Calls err-proc if an exception occurs in proc. The exception is passed to err-proc
>(on-err (fn (ex) (string "caught " (details ex))) (fn () (/ 1 0))) "caught /: division by zero"
Executes expr, returning nil if an error occurs.
>(errsafe (/ 1 2)) 1/2
>(errsafe (/ 1 0)) nil
Copyright 2008 Ken Shirriff.