[issue34538] Remove encouragement to author a base class for all Exception subclasses in a module
New submission from Nathaniel Manista
INADA Naoki
Raymond Hettinger
INADA Naoki
Also, it mirrors practices in the standard library (decimal.DecimalException and sqlite3.DatabaseError).
As Nathaniel said, "it's good idea in the particular circumstances of a particular module's implementation, but generally not." (I removed "very" because it is too noisy) For example, socket.error was migrated into OSError. So not having base exception class for module is also "practices in the standard library".
For users, it provides a way to catch possible errors that are specific to a particular module.
For tutorial readers, caching errors specific to particular cause should be suggested, instead of particular module.
"How/When custom base exception class is useful" is very high level topic.
It's too difficult for tutorial.
If tutorial reader start using base exception class without any real benefit, it will lead ugly code like this:
try:
# some code here
except ValueError as e:
raise CustomValueError(e.args)
except TypeError as e:
raise CustomTypeError(e.args)
except ...
----------
_______________________________________
Python tracker
Brett Cannon
Raymond Hettinger
Brett Cannon
Ammar Askar
INADA Naoki
INADA Naoki
https://github.com/search?q=%22except+TemplateError%22&type=Code
For example, I found flask_mako's TemplateException in this search result.
Strictly speaking, this is not base exception class. It is wraps exception during template rendering. But "why this class is useful?" is very similar to base exception class.
It provides better traceback for mako template. They use this class for "particular reason", not because it's generally recommended practice.
And this "particular reason" shouldn't be in Python tutorial, clearly.
---
If we really need exception class hierarchy in tutorial, I think OSError is the best example. When opening a file, `except OSError:` is much better than `except (PermissionError, FileNotFound, ...)`. It's the most clear example when common base class for some distinct exceptions is useful.
----------
_______________________________________
Python tracker
Nathaniel Manista
INADA Naoki
Raymond Hettinger
Guido van Rossum
Marc-Andre Lemburg
Change by Irit Katriel
participants (8)
-
Ammar Askar
-
Brett Cannon
-
Guido van Rossum
-
INADA Naoki
-
Irit Katriel
-
Marc-Andre Lemburg
-
Nathaniel Manista
-
Raymond Hettinger