all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* About `catch' and `throw'
@ 2012-12-21 12:00 Xue Fuqiao
  2012-12-21 17:03 ` Drew Adams
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Xue Fuqiao @ 2012-12-21 12:00 UTC (permalink / raw)
  To: help-gnu-emacs

I'm reading the Emacs Lisp Reference Manual, and I met a problem.  In the node 10.5.1(http://www.gnu.org/software/emacs/manual/html_node/elisp/Catch-and-Throw.html#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?

English is not my native language; please excuse typing errors.
-- 
Best regards.



^ permalink raw reply	[flat|nested] 8+ messages in thread

* RE: About `catch' and `throw'
  2012-12-21 12:00 About `catch' and `throw' Xue Fuqiao
@ 2012-12-21 17:03 ` Drew Adams
  2012-12-22 17:50   ` Stefan Monnier
       [not found]   ` <mailman.15933.1356198906.855.help-gnu-emacs@gnu.org>
  2012-12-21 19:08 ` Pascal J. Bourguignon
       [not found] ` <mailman.15880.1356116940.855.help-gnu-emacs@gnu.org>
  2 siblings, 2 replies; 8+ messages in thread
From: Drew Adams @ 2012-12-21 17:03 UTC (permalink / raw)
  To: 'Xue Fuqiao', help-gnu-emacs

> 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.




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: About `catch' and `throw'
  2012-12-21 12:00 About `catch' and `throw' Xue Fuqiao
  2012-12-21 17:03 ` Drew Adams
@ 2012-12-21 19:08 ` Pascal J. Bourguignon
       [not found] ` <mailman.15880.1356116940.855.help-gnu-emacs@gnu.org>
  2 siblings, 0 replies; 8+ messages in thread
From: Pascal J. Bourguignon @ 2012-12-21 19:08 UTC (permalink / raw)
  To: help-gnu-emacs

Xue Fuqiao <xfq.free@gmail.com> writes:

> I'm reading the Emacs Lisp Reference Manual, and I met a problem.  In the node 10.5.1(http://www.gnu.org/software/emacs/manual/html_node/elisp/Catch-and-Throw.html#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?

The point is that catch/throw is a dynamic mechanism, not a lexical one.
So it is bad to say "outside", since this has a spacial conotation.

throw can be used WHEN catch is executing, or WHEN it is not.

If you call (throw 'something result) WHEN there's a (catch 'something …)
executing, then the control will pass from the throw expression to the
catch expression, and the catch expression will return the result
passed to throw.

It's only a question of time, of WHEN catch is being evaluated and WHEN
throw is evaluated.




I could talk you about block and return-from which are a lexical
mechanism, with which what matters is WHERE the return-from is relative
to the lexical scope defined by block, but the current implementation
(even in emacs-24) is full of bugs.  If you want, ask again on
news:comp.lang.lisp and we'll tell you about catch/throw and
block/return-from in Common Lisp.

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
A bad day in () is better than a good day in {}.




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: About `catch' and `throw'
  2012-12-21 17:03 ` Drew Adams
@ 2012-12-22 17:50   ` Stefan Monnier
       [not found]   ` <mailman.15933.1356198906.855.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 8+ messages in thread
From: Stefan Monnier @ 2012-12-22 17:50 UTC (permalink / raw)
  To: help-gnu-emacs

> During the evaluation of code that is lexically inside the `catch',
> a `throw' that is evaluated throws back to that `catch'.

Usually, but not always.

> 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.

Usually, but not always:

  (catch 'foo
    (let ((f (lambda () (throw 'foo))))
      ...
      (catch 'foo
        (funcall f))
      ...))

will throw to the inner catch, not the outer one.


        Stefan




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: About `catch' and `throw'
       [not found]   ` <mailman.15933.1356198906.855.help-gnu-emacs@gnu.org>
@ 2012-12-22 20:33     ` Pascal J. Bourguignon
  0 siblings, 0 replies; 8+ messages in thread
From: Pascal J. Bourguignon @ 2012-12-22 20:33 UTC (permalink / raw)
  To: help-gnu-emacs

Stefan Monnier <monnier@iro.umontreal.ca> writes:

>> During the evaluation of code that is lexically inside the `catch',
>> a `throw' that is evaluated throws back to that `catch'.
>
> Usually, but not always.
>
>> 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.
>
> Usually, but not always:
>
>   (catch 'foo
>     (let ((f (lambda () (throw 'foo))))
>       ...
>       (catch 'foo
>         (funcall f))
>       ...))
>
> will throw to the inner catch, not the outer one.

Again, because it does not matter WHERE the catch is: it's not because
it's _placed_ _inner_ or _outer_.

What matter is WHEN the catch is: it is because it is the most recently
established catch 'foo, that it will catch the throw 'foo.  Conversely
with:

   (catch 'foo
     (funcall (catch 'foo
                 (lambda () (throw 'foo)))))

it is the first catch 'foo which is the most RECENTLY established catch
'foo, WHEN throw 'foo is called, because the second catch 'foo at that
TIME has been already disestablished.


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
A bad day in () is better than a good day in {}.


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: About `catch' and `throw'
       [not found] ` <mailman.15880.1356116940.855.help-gnu-emacs@gnu.org>
@ 2013-01-05  0:57   ` David Combs
  2013-01-08 16:08     ` Stefan Monnier
       [not found]     ` <mailman.16939.1357661400.855.help-gnu-emacs@gnu.org>
  0 siblings, 2 replies; 8+ messages in thread
From: David Combs @ 2013-01-05  0:57 UTC (permalink / raw)
  To: help-gnu-emacs

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1238 bytes --]

In article <mailman.15880.1356116940.855.help-gnu-emacs@gnu.org>,
Pascal J. Bourguignon <pjb@informatimago.com> wrote:
>...

>The point is that catch/throw is a dynamic mechanism, not a lexical one.
>So it is bad to say "outside", since this has a spacial conotation.
>
>throw can be used WHEN catch is executing, or WHEN it is not.
>
>If you call (throw 'something result) WHEN there's a (catch 'something …)
>executing, then the control will pass from the throw expression to the
>catch expression, and the catch expression will return the result
>passed to throw.
>
>It's only a question of time, of WHEN catch is being evaluated and WHEN
>throw is evaluated.
>
Would you then say that when throw is triggered, it starts looking
up the stack (to older items) for a (matching?) catch, then collapses
the part of the stack it came from (where the throw was) up to
that point, fixes things up, and continues within the catch?

And if there is no catch, then it does <you fill in the answer>.


>
>
>
...., but the current implementation
>(even in emacs-24) is full of bugs.  

In your opinion, is this going to get fixed anytime soon?

Or has it been there for decades, and probably will remain
for decades to come?


Thanks,

David




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: About `catch' and `throw'
  2013-01-05  0:57   ` David Combs
@ 2013-01-08 16:08     ` Stefan Monnier
       [not found]     ` <mailman.16939.1357661400.855.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 8+ messages in thread
From: Stefan Monnier @ 2013-01-08 16:08 UTC (permalink / raw)
  To: help-gnu-emacs

>> ...., but the current implementation
>> (even in emacs-24) is full of bugs.

I couldn't find the actual article to see the context.  Was it saying
that catch&throw are full of bugs?  Or was it referring to
something else?


        Stefan "not aware of bugs in catch&throw"




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: About `catch' and `throw'
       [not found]     ` <mailman.16939.1357661400.855.help-gnu-emacs@gnu.org>
@ 2013-02-13  1:08       ` David Combs
  0 siblings, 0 replies; 8+ messages in thread
From: David Combs @ 2013-02-13  1:08 UTC (permalink / raw)
  To: help-gnu-emacs

In article <mailman.16939.1357661400.855.help-gnu-emacs@gnu.org>,
Stefan Monnier  <monnier@iro.umontreal.ca> wrote:
>>> ...., but the current implementation
>>> (even in emacs-24) is full of bugs.
>
>I couldn't find the actual article to see the context.  Was it saying
>that catch&throw are full of bugs?  Or was it referring to
>something else?
>
>
>        Stefan "not aware of bugs in catch&throw"
>
>

Don't know.  It's a whole month later (my fault).

But thanks!

David




^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2013-02-13  1:08 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-12-21 12:00 About `catch' and `throw' Xue Fuqiao
2012-12-21 17:03 ` Drew Adams
2012-12-22 17:50   ` Stefan Monnier
     [not found]   ` <mailman.15933.1356198906.855.help-gnu-emacs@gnu.org>
2012-12-22 20:33     ` Pascal J. Bourguignon
2012-12-21 19:08 ` Pascal J. Bourguignon
     [not found] ` <mailman.15880.1356116940.855.help-gnu-emacs@gnu.org>
2013-01-05  0:57   ` David Combs
2013-01-08 16:08     ` Stefan Monnier
     [not found]     ` <mailman.16939.1357661400.855.help-gnu-emacs@gnu.org>
2013-02-13  1:08       ` David Combs

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.