如果我有一个异常类的继承层次结构,并且在try
块中,如果我想在更一般的异常之前处理更具体的(派生的)异常,我是否只将派生类的except
语句放在基类的语句之上?在
我会这样做吗?在
class MyException(BaseException):
pass
class AnotherException(MyException):
pass
try:
raise AnotherException()
except AnotherException:
print('Caught YetAnotherException!')
except MyException:
print('Caught MyException!')
print('Done.')
我已经试过了,而且很有效,但是我很惊讶我找不到任何关于这个的文档。在
查看Python语言参考中^{} statement 的文档。相关位为:
一些常用的异常子句形式带有try语句
相关问题 更多 >
编程相关推荐