<分区>
我想在我自己的类中嵌套一个 Exception 子类,如下所示:
class Foo(object):
def bar(self):
#does something that raises MyException
class MyException(Exception):
pass
这样,当从另一个模块调用 bar() 时,我只需要导入 Foo(而不是 MyException)。但是我下面的内容不起作用:
from foo_module import Foo
foo = Foo()
try:
foo.bar()
except Foo.MyException as e:
print e
Python 给出了这个错误:
type object 'Foo' has no attribute 'MyException'
有办法吗?