Catch clearly defined exception from sub.submodule in python
I have 3 files. xxx which imports xxx2 and xxx2 imports xxx3 which one raises OppsError exception.
xxx3.py:
class OppsError(Exception):pass
def go():
raise OppsError()
xxx2.py:
import xxx3
xxx3.go()
xxx.py:
try:
import xxx2
except xxx3.OppsError:
print 'ops'
When i run xxx.py i get error NameError: name 'xxx3' is not defined
. Is importing 开发者_JAVA技巧xxx3 inside xxx only way to catch OppsError
?
As far as I know, it is (unless you are willing to replace OppsError
with a built-in exception that is already known to xxx
or to catch a more general exception instead of OppsError
from which OppsError
is derived).
精彩评论