分析源码发现 BaseException 是 Exception 的父类,作为子类的Exception无法截获父类BaseException类型的错误
class BaseError(BaseException):
pass
try:
raise BaseError
except: # "except:" 是 "except BaseException:" 的省略写法
print('BaseError 被 except: 捕获成功')
try:
raise BaseError
except BaseException:
print('BaseError 被 except BaseException: 捕获成功')
try:
raise BaseError
except Exception:
print(''BaseError 被 except Exception: 捕获成功') # 此处不会打印因为捕获失败了
print('结束')
class Error(Exception):
pass
try:
raise Error
except Exception:
print('Error 被 Exception 捕获成功')
print('结束')
网友评论