Can someone please explain why the following code raises an error?
error = None
try:
0 / 0
except ZeroDivisionError as error:
pass
# Do some post-processing...
if error is not None: # NameError: name 'error' is not defined
raise error
The second-last line crashes with an error:
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-69-7ce6d086be88> in <module>()
8 # Do some post-processing
9
---> 10 if error is not None:
11 raise error
NameError: name 'error' is not defined
It seems that catching an error as a variable removes that variable from the stack after the try / except
clause, but this seems very weird to me...