[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: About `catch' and `throw'
From: |
Drew Adams |
Subject: |
RE: About `catch' and `throw' |
Date: |
Fri, 21 Dec 2012 09:03:19 -0800 |
> I'm reading the Emacs Lisp Reference Manual, and I met a
> problem. In the node `Catch and Throw', it says:
>
> throw is used inside a catch, and jumps back to that catch.
> For example:
> (defun foo-outer ()
> (catch 'foo
> (foo-inner)))
> (defun foo-inner ()
> ...
> (if x
> (throw 'foo t))
> ...)
>
> but the `throw' is used outside the `catch', I'm confused.
> Can anybody help?
It's about dynamic extent (scope, if you like) vs lexical scope. It should
perhaps not say "inside", as that is a bit ambiguous. It really means that the
`throw' is evaluated during the evaluation of the sexp inside the `catch'. The
`throw' need not be lexically inside the `catch'.
During the evaluation of code that is lexically inside the `catch', a `throw'
that is evaluated throws back to that `catch'. And if there is more than one
`catch' with the same label then it throws to the one that is lexically nearest
(i.e., innermost, outside the `throw'), which is also the most recent.
This part of the text should make it clear:
"The `throw' need not appear lexically within the `catch' that
it jumps to. It can equally well be called from another function
called within the `catch'. As long as the `throw' takes place
chronologically after entry to the `catch', and chronologically
before exit from it, it has access to that `catch'."
That last sentence says it better than I have said it here. It's really about
dynamic extent ("chronologically"), not lexical scope.