[Grace-core] try ... finally

Andrew Black black at cs.pdx.edu
Wed Jul 18 14:47:31 PDT 2012


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.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: TryFinally.grace
Type: application/octet-stream
Size: 1242 bytes
Desc: not available
URL: <https://mailhost.cecs.pdx.edu/mailman/private/grace-core/attachments/20120718/425d85ee/attachment.obj>
-------------- next part --------------


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?

	Andrew



More information about the Grace-core mailing list