如何在python中处理异常

时间:2016-05-23 08:49:43

标签: python exception-handling

我收到这样的错误

return SegmentWriter(self, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/whoosh/writing.py", line 502, in __init__
    raise LockError
whoosh.index.LockError

我想用try / except语句捕获这些错误。所以我写了

try:
    do whatever causes the error
except LockError:
    print "LockError..."
    handle error

但这导致NameError,因为LockError未知?

    except LockError:
NameError: global name 'LockError' is not defined

如何处理这些锁定错误?

1 个答案:

答案 0 :(得分:4)

您的命名空间中的第一个导入异常。将其添加到您的模块:

from whoosh.index import LockError