[Tutor] Callbacks and exception handling
Alan Gauld
alan.gauld at btinternet.com
Fri Aug 13 10:23:19 CEST 2010
"Pete" <pkoek11 at xs4all.nl> wrote
> One thing though - I noticed that when an exception is raised in the
> callback function, that exception doesn't actually "show up" in the
> calling program.
Yes it does and Steven has answered that so maybe you are clear now.
But just to clarify, when you set a callback there is no guarantee
that
the callback gets used immediately so the exception will not
necessarily
appear in the setting function, it will appear in the calling
function.
cb = []
def setcb(f):
cb.append(f)
def fireworks():
try:
for f in cb: print f() # call them all now
except:
print 'a callback failed'
def myFunc()
setcb('lambda : len(cb))
setcb('lambda : 42)
setcb('lambda : 66)
setcb('lambda : 12/0) #oops!
myFunc()
fireworks()
Here any exceptions in the callback functions won't appear until
the fireworks function, they will not show up in myFunc.
HTH,
--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/
More information about the Tutor
mailing list