In python, whenever we are writing User-defined exception, we have to extend it from class Exception
.
my question is why can't we extend it from BaseException
which is super-class of exception hierarchy and Exception
is also subclass of BaseException
.
BaseException
includes things like KeyboardInterrupt
and SystemExit
, which use the exception mechanism, but which most people shouldn't be catching. It's analogous to Throwable
in Java, if you're familiar with that. Things that derive directly from BaseException
are generally intended to shut down the system while executing finally
blocks and context manager __exit__
methods to release resources.