[squeak-dev] order of ensure: processing vs. error handling

Chris Muller ma.chris.m at gmail.com
Mon Sep 5 19:30:23 UTC 2016

Hi all, I have some in this code in which I try to read a file and, if
it fails, try to read it in a different way.

But the order of processing ensure: blocks vs. error-handler blocks is
not what I expected.  I expected the ensure: block of the inner
handler to run before the outer handler.

Here's a simple workspace to demonstrate. The temp var "open"
represents the status of a FileStream.

|open| open:=nil.
World findATranscript: nil  Transcript clear.
[ [ [ "try method1"  open:=true.  Error signal] ensure: [ open:=false] ]
    on: Error
        [ : innerErr |   "try method2, but file still open!"
        Transcript cr; show: 'innerErr handler, open is ', open asString.
        innerErr return:
            ([ open:=true.
            Error signal ] ensure: [ open:=false ]) ] ]

    on: Error
    do: [ : outerErr | Transcript cr; show: 'outerErr handler, open is
', open asString ].
Transcript cr; show: 'Done.  open is ', open asString.

It seems none of the ensure blocks are processed until after ALL the
error-handler blocks.  So if a #fileNamed:do: deep deep in the stack
encounters an Error, the file is still open in the upper-level
handlers, with no good way to close it..

More information about the Squeak-dev mailing list