[issue46291] [doc] First argument to raise can also be BaseException

Andre Roberge report at bugs.python.org
Fri Jan 7 16:10:25 EST 2022


Andre Roberge <andre.roberge at gmail.com> added the comment:

Irit:


In all the books and tutorials I have seen, the advice is to try to catch specific exceptions whenever possible or, *at most*, to catch Exception (and not BaseException).  This is to allow, for example, a program to be interrupted by a KeyboardInterrupt.

As you know, the hierarchy is as follows:

BaseException
 +-- SystemExit
 +-- KeyboardInterrupt
 +-- GeneratorExit
 +-- Exception
      +-- all others

If specific action to do some cleanup before a SystemExit (usually the result of calling sys.exit()) or catching some KeyboardInterrupt (which is generally NOT done via a raise statement), then these specific exception should be caught.
The documentation refers to GeneratorExit as not indicating an error needing to be caught by users.

For this advice (catching Exception and not BaseException) to be correct, users should be advised (as they are in the tutorial) to raise exceptions derived from Exception (and not BaseException).

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue46291>
_______________________________________


More information about the Python-bugs-list mailing list