Capturing non-local returns?

Stephen Pair stephen at pairhome.net
Sat May 24 17:52:28 UTC 2003


Avi Bryant wrote:

>On Sat, 24 May 2003, Andreas Raab wrote:
>
>  
>
>>I was wondering if there's a good way of capturing non-local returns and
>>ONLY non-local returns from some block. E.g., the problem I'm having is that
>>I am evaluating a block and if (and ONLY if) it attempts to do a non-local
>>return I want to capture the return and take some appropriate action.
>>    
>>
>
>Will this not work?
>
>|normalReturn|
>normalReturn := false.
>[aBlock value.
> normalReturn := true]
>   ensure: [normalReturn ifFalse: [...]]
>  
>

I believe (as Anthony said) that this is the same as #ifCurtailed:.  I 
think the following method does the trick:

-----
BlockContext>>onNonLocalReturnDo: aBlock

	| gotError answer |
	gotError := false.
	[[answer := self value] on: Error do: 
		[ :ex |
		gotError := true.
		ex pass]]
			ifCurtailed:
				[ gotError ifFalse: [aBlock value]].
	^answer
----

- Stephen




More information about the Squeak-dev mailing list