我有以下代码示例,
class Outer:
class InnerException(Exception): pass
def raise_inner(self):
raise Outer.InnerException()
def call_raise(self):
try:
self.raise_inner()
except Outer.InnerException as e:
print "Handle me"
Outer().call_raise()
我想做的是不要在外部类中使用
Outer.InnerException
,简而言之就是这样class Outer:
class InnerException(Exception): pass
#InnerException = Outer.InnerException .. something like this, create some alias
def raise_inner(self):
raise InnerException()
def call_raise(self):
try:
self.raise_inner()
except InnerException as e:
print "Handle me"
Outer().call_raise()
最佳答案
不能只引用在类作用域中用名称声明的内容(除非您也在类作用域中这样做)。
或者:
class Outer:
class InnerException(Exception): pass
def raise_inner(self):
raise self.InnerException()
或
class InnerException(Exception): pass
class Outer:
def raise_inner(self):
raise InnerException()