[Grace-core] try ... finally

Michael Homer mwh at ecs.vuw.ac.nz
Wed Jul 18 17:04:02 PDT 2012


On Thu, Jul 19, 2012 at 9:47 AM, Andrew Black <black at cs.pdx.edu> wrote:
> Michael,
>
> I've been trying to use try {} else {} to write try {} finally {}.  The idea is that the finally block is always
> executed, and its value discarded, whether or not the try block returns normally.  The value of the try {} finally {} is the value of the try block
>
> Here is my best attempt so far, with some tests.
>
>
>
>
> They more or less work, except for  this one
>
> method testTryFinallyWithReturn {
>     try {
>         print "started testTryFinallyWithReturn"
>         return
>     } finally { print "4: finalizaed after early return" }
> }
>
> where the finally block is not executed.
>
> The other problem is that in this test
>
> method testTryFinallyWithError {
>     try {
>         print "started testTryFinallyWithError"
>         forceError "error forced"
>     } finally { print "5: finalization happened" }
> }
>
> the error that is eventually signalled is not "error forced", but another error forced in the implementation of try {} finally{}.
>
> I can't see how to do better with the existing primitives.  Can you?  Or do we need to make try{}finally{} a primitive?
>
> I'm wondering if try{}else{} might be better named try{}onError{}, and if "forceError" might be called just "error"?
>
> What do you think?
At some, perhaps distant, point it needs to learn exceptions and do
catch-case properly. The try-else hack explicitly doesn't and can't
deal with non-local returns and can't be nested (the program will
eventually crash in both cases, when it jumps to an arbitrary piece of
stack). There isn't any way to deal with non-local returns now.

catch-case might well have a finally block too, and the example in the
specification does include one. It also mentions a do()finally() for
skipping the exception handling.
-Michael


More information about the Grace-core mailing list