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
.
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
BaseException
includes things likeKeyboardInterrupt
andSystemExit
, which use the exception mechanism, but which most people shouldn't be catching. It's analogous toThrowable
in Java, if you're familiar with that. Things that derive directly fromBaseException
are generally intended to shut down the system while executingfinally
blocks and context manager__exit__
methods to release resources.Per the Python2 documentation, there are four exceptions that are derivatives of
BaseException
:The three that are not
Exception
are not actually errors, which means in general you don't want to catch them as if they are errors.BaseException
was added in Python2.5 (before that, there was noBaseException
and the other exceptions were subclassed fromException
).