I\'ve got a pretty good understanding of python\'s try-except clause, but I\'m encountering problems when trying to put it inside of a function.
>>>
tryAppend(foo, var1)
is evaluated (roughly) in this order:
tryAppend
referencesfoo
referencesvar1
referencestryAppend
does, uncluding the try-except)The error occurs at #2, long before the function and the try block is entered. In fact, the try block cannot to throw a NameError, as the only names used are parent
and child
, both being arguments and thus always available (if .append
does not exist, that's an AttributeError
).
In the same way, the following code will not print "caught it" because the exception is raised before the try block is executed:
raise Exception("Catch me if you can")
try:
pass # do nothing
except:
print "caught it"