[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: ‘with-exception-handler’ rational e
From: |
Neil Jerram |
Subject: |
Re: ‘with-exception-handler’ rational e |
Date: |
Fri, 07 Mar 2014 14:22:02 +0000 |
User-agent: |
Roundcube Webmail/0.9.5 |
On 2014-03-07 13:34, Nikita Karetnikov wrote:
I’ve expected ‘with-exception-handler’ to behave like ‘catch’, but it
doesn’t.
scheme@(guile-user)> ,use (srfi srfi-34)
scheme@(guile-user)> (with-exception-handler (const "got it") (lambda
() (raise "boom!")))
ice-9/boot-9.scm:106:20: In procedure #<procedure 94b0e40 at
ice-9/boot-9.scm:97:6 (thrown-k . args)>:
ice-9/boot-9.scm:106:20: Throw to key `srfi-34' with args `("boom!")'.
Entering a new prompt. Type `,bt' for a backtrace or `,q' to continue.
scheme@(guile-user) [1]>
scheme@(guile-user)> (catch 'misc-error (lambda () (error "boom!"))
(const "got it"))
$1 = "got it"
Is there an explanation that doesn’t involve diving into the land of
continuations and dynamic wind?
Yes: the HANDLER that you specify in '(catch TAG THUNK HANDLER)' is
implicitly extended, by Guile, by a non-local jump to the continuation
of the '(catch ...)'. The HANDLER that you specify in
'(with-exception-handler HANDLER THUNK)' isn't.
So, to get catch-like behaviour you'd need to write something like this:
(call/cc (lambda (k)
(with-exception-handler (lambda (e)
(k "got it"))
(lambda () (raise "boom!")))))
i.e. explicitly call the escape continuation in your handler code.
Why? I think, simply because:
- the catch behaviour is traditional for catch/throw in Lisp and Scheme
- the with-exception-handler is what SRFI-34 specifies.
Alternatively you can use SRFI-34's 'guard' form, which encapsulates a
pattern like that.
Hope that helps,
Neil