为什么我们应该使用Exception作为超类,为什么不使用BaseException

tai*_*raj 5 python exception superclass python-2.7

在python中,每当我们编写用户定义的异常时,我们都必须从类中扩展它Exception.我的问题是为什么我们不能扩展它的BaseException超类异常层次结构,Exception也是它的子类BaseException.

use*_*ica 9

BaseException包括像KeyboardInterruptSystemExit使用异常机制的东西,但大多数人不应该抓住它.Throwable如果您熟悉它,它与Java 类似.直接派生的东西BaseException通常用于在执行finally块和上下文管理器__exit__方法时释放资源来关闭系统.