The catch and throw structures are used to perform
explicit transfers of control. First a catch form is used to
setup a tag, this acts like a label for the C language's goto
statement. To transfer control a throw form is then used to
transfer to the named tag. The tag is destroyed and the catch
form exits with the value provided by the throw.
In a program this looks like,
(catch 'tag
;; Forms which may `throw' back to tag
...
(throw 'tag value)
;; Control has now passed to the `catch',
;; no more forms in this progn will be evaluated.
...)
=> value
where tag is the tag to be used (this is normally a symbol) and
value is the result of the catch form.
When a throw actually happens all catches in scope are searched for
one with a tag which is eq to the tag in the throw. If more than
one exists the most-recent is chosen. Now that the catch has been located
the environment is `wound-back' to the catch's position (i.e. local variables
are unbound, cleanup forms removed, unused catches forgotten, etc...)
and all Lisp constructs between the current point of control and
the catch are exited.
For example,
(let
((test 'outer))
(cons (catch 'foo
(let
((test 'inner))
(throw 'foo test)
(setq test 'unreachable))) ;Never reached
test))
=> (inner . outer)
when the throw executes the second binding of test is unwound and
the first binding comes back into effect. For more details on variable
binding see section Local Variables.
Note that catch tags are dynamically scoped, the thrower does not have to be within the same lexical scope (this means you can throw through functions).
tag is evaluated and recorded as the tag for this catch. Next the
body-forms are evaluated as an implicit progn. The value of the
catch form is either the value of the progn, or, if a throw
happened, the value specified in the throw form.
Before exiting the tag installed by this form is removed.
eq to tag. The value returned by this catch
form is either catch-value or nil if catch-value is
undefined.
If there is no catch with a tag of tag an error is signalled and the editor returns to the top-level of evaluation.
Go to the first, previous, next, last section, table of contents.