在try / except块之外保留对错误的引用

时间:2017-12-17 22:18:32

标签: python

有人可以解释为什么以下代码会引发错误吗?

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

倒数第二行崩溃并出现错误:

---------------------------------------------------------------------------
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

似乎将一个错误作为一个变量从try / except子句后的堆栈中删除该变量,但这对我来说似乎很奇怪......

0 个答案:

没有答案