[Vm-dev] Handling exceptions during callback

Igor Stasenko siguctua at gmail.com
Mon May 3 23:31:41 UTC 2010


Hello,
Andreas and Eliot,

i direct my question mainly to you , because you are most experienced
in this area, but if there's others who want to share their thoughts,
please do so.

So, the question is, what to do, if an error triggered during a callback.
My concern is, that sometimes its unsafe to do return from callback
with invalid value.

For example, if callback's caller expects a pointer to something,
returning a default value (NULL)
will have the same effect as simply terminating VM :)

Here's my snippet of code, which i coded so far:

block: aBlock

	self numberOfArguments = aBlock numArgs ifFalse: [
		self error: 'Invalid number of arguments' ].
	
	context := [	"here we entering a callback"
 	    [
		[ | args |  args := self readArguments.
		  self leave: (aBlock valueWithArguments: args) ]
			ifCurtailed: [ self error: 'attempt to non-local return across a callback' ]
		
	    ] repeat.
	] asContext


when callback is activated, an interpreter should enter the context ,
stored in callback's context ivar.

The best what i invented so far, is to wrap the whole thing in repeat block.
So, if there's error happens, there could be at least chance that user
(like me ;) might supply the right argument to the #leave:  method,
using debugger.
A leave: method serves to leave callback , but also might fail, if it
can't coerce a return value to corresponding C equivalent.

Of course, there's many other variants, like
self leave: ( [ aBlock valueWithArguments: args ] ifCurtailed: [ self
askUserToEnterTheValue ] )

i'd like to know, what you think would be the best variant.


-- 
Best regards,
Igor Stasenko AKA sig.


More information about the Vm-dev mailing list