carl carl - 2 years ago 101
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/", line 502, in __init__
raise LockError

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

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

but this leads to a
, since
is unknown?

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

how do I handle these Lock errors?

Answer Source

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

from whoosh.index import LockError
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download