Message410035
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). |
|
Date |
User |
Action |
Args |
2022-01-07 21:10:25 | aroberge | set | recipients:
+ aroberge, docs@python, sobolevn, iritkatriel, gtitze |
2022-01-07 21:10:25 | aroberge | set | messageid: <1641589825.64.0.241500879372.issue46291@roundup.psfhosted.org> |
2022-01-07 21:10:25 | aroberge | link | issue46291 messages |
2022-01-07 21:10:25 | aroberge | create | |
|