|
Brent Pinkney wrote:
> Hi,
>
> What is the expected result of
>
> [ 1 / 0 ] ifCurtailed: [ 2 ]
>
> I had thought #ifCurtailed: was used to catch any exceptions?
No, #ifCurtailed: is used to execute cleanup code *if the execution of
the method won't continue past the #ifCurtailed: itself*. This can
surely happen because of an exception, but also because the block, or
something it calls, does a return-from-method. For example,
[ ^1 ] ifCurtailed: [ Transcript show: 'yeppp' ].
will execute the #ifCurtailed: block too.
#ifCurtailed: could be implemented like this in terms of #ensure:
ifCurtailed: curtailBlock
| result curtailed |
curtailed := true.
[ result := self value. curtailed := false ] ensure: [
curtailed ifTrue: [ curtailBlock value ] ].
^result
Dually, #ensure: could be implemented like this using #ifCurtailed:
ensure: ensureBlock
| result |
result := self ifCurtailed: ensureBlock.
"If we come here, execution was not curtailed and
ensureBlock has not been executed yet."
ensureBlock value.
^result
Paolo
|