[plt-scheme] finally

From: Eli Barzilay (eli at barzilay.org)
Date: Wed Dec 21 16:14:26 EST 2005

On Dec 21, Doug Orleans wrote:
> Eli Barzilay writes:
>  > If you're already going down to the `with-handlers' level, why not
>  > just use it for the finally clause?  Something like:
>  > 
>  >   (define-syntax try
>  >     (syntax-rules (finally)
>  >       ((_ expr catch-clause ... (finally finally-expr ...))
>  >        (let ([finalizer (lambda () finally-expr ...)])
>  >          (with-handlers ((exn? (lambda (exn) (finalizer) catch-expr ...))
>  >                          ...
>  >                          (void (lambda (e) (finalizer) (raise e))))
>  >            (begin0 expr (finalizer)))))))
>  > 
>  > (which is a bit different than your version.)
> 
> Yes, it's a bit different from the Java semantics as well:
> 
> http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.20.2
> 
>  * The finally-exprs should be evaluated at the end of each
>    catch-clause, not the beginning.
>  * If there's an exception during any catch clause, the finally-exprs
>    should still be evaluated.

OK, fixing it is easy enough, I wasn't sure whether that was
intentional or not...


> But I think you're right, with-handlers is better than using
> dynamic-wind, because it will only catch exceptions and not other
> transfers of control.  For example, if the try expression is opening
> a port, you want to close the port if the expression completes
> normally or if there's an exception, but if it jumps to another
> continuation and then jumps back you want to leave it open.

That's a sticky issue -- I think that there was a discussion about
this on c.l.s a few years ago, with all kinds of suggestions like
weird ways of jumping to a continuation in a way that bypasses
dynamic-wind.


> Hm, what about escape continuations?  It would be reasonable to
> close the port in this case, since control will never come back.  Is
> there a way for dynamic-wind (or something else) to differentiate
> between escape continuations and full continuations?

No.

-- 
          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!


Posted on the users mailing list.