carl carl - 6 months ago 19
Python Question

how to handle exceptions in python

I get errors like this

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


I would like to catch these errors with a try/except statement. So I wrote

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


but this leads to a
NameError
, since
LockError
is unknown?

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


how do I handle these Lock errors?

Answer

First import exception in your namespace. Add this to your module:

from whoosh.index import LockError