catch
catch tag {form}* → {result}*
7.10.0 3tag—a catch tag; evaluated.
forms—an implicit progn.
results—if the forms exit normally, the values returned by the forms; if a throw occurs to the tag, the values that are thrown.
!!! Barmar: Tags are compared using EQ, not EQL. Therefore, the consequences of using numbers and characters as tags are unspecified. KMP: Personally, I think we should fix that to simplify the language description. I don't approve of our using EQ anywhere in the spec.
7.10.0 2catch is used as the destination of a non-local control transfer by throw. Tags are used to find the catch to which a throw is transferring control. (catch 'foo form) catches a (throw 'foo form) but not a (throw 'bar form).
7.10.0 4The order of execution of catch follows:
catch.
progn, and the results of the last form are returned unless a throw occurs.
throw occurs during the execution of one of the forms, control is transferred to the catch form whose tag is eq to the tag argument of the throw and which is the most recently established catch with that tag. The evaluation of \param{forms} is aborted.No further evaluation of forms occurs. Barmar thinks this is not redundant. I agree. -kmp 22-Dec-90
If several tags match the tag
argument of a \specref{throw},
control is transferred to the dynamically most recently established
\specref{catch}.
catch is disestablished just before the results are returned.
7.9.2 14
7.9.2 15
gray's rewordingIf during the execution of one of the forms, a throw is executed whose tag is eq to the catch tag, then the values specified by the throw are returned as the result of the dynamically most recently established catch form with that tag. If a \specref{throw} occurs
during the execution of one of the \param{forms},
and there is a \specref{catch} \param{tag} that is
\funref{eq} to the \specref{throw} tag,
the value or values resulting from
the \specref{throw} are returned.
The mechanism for catch and throw works even if throw is not within the lexical scope of catch. throw must occur within the dynamic extent of the evaluation of the body of a catch with a corresponding tag.
(catch 'dummy-tag 1 2 (throw 'dummy-tag 3) 4) → 3
(catch 'dummy-tag 1 2 3 4) → 4
(defun throw-back (tag) (throw tag t)) → THROW-BACK
(catch 'dummy-tag (throw-back 'dummy-tag) 2) → T
;; Contrast behavior of this example with corresponding example of BLOCK.
(catch 'c
(flet ((c1 () (throw 'c 1)))
(catch 'c (c1) (print 'unreachable))
2)) → 2
None.
control-error is signaled if throw is done when there is no suitable catch tag.
throw, Section 3.1 (Evaluation)
It is customary for symbols to be used as tags, but any object is permitted. However, numbers should not be used because the comparison is done using eq.
catch differs from block in that catch tags have dynamic scope while block names have lexical scope.