SystemExit is raised when
sys.exit() is called.
KeyboardInterrupt is raised when the user asks the program
to stop by pressing interrupt keys. Both exceptions are expected to propagate up until the application stops.
In order to avoid catching
KeyboardInterrupt by mistake PEP-352 created the root class
BaseException from which
Exception derive. Thus developers can use
without preventing the software from stopping.
GeneratorExit class also derives from
BaseException as it is not really an error and is not supposed to be caught by
As said in Python’s documentation, user-defined exceptions are not
supposed to inherit directly from
BaseException. They should instead inherit from
Exception or one of its subclasses.
This rule raises an issue when a class derives from one of the following exception classes:
Noncompliant Code Example
class MyException(BaseException): # Noncompliant
class MyException(GeneratorExit): # Noncompliant
class MyException(KeyboardInterrupt): # Noncompliant
class MyException(SystemExit): # Noncompliant