Author Nathaniel Manista
Recipients Nathaniel Manista, docs@python
Date 2018-08-28.20:40:50
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1535488851.13.0.56676864532.issue34538@psf.upfronthosting.co.za>
In-reply-to
Content
https://docs.python.org/3.8/tutorial/errors.html (and all other versions of that page back at least as far as 2.7) currently contain the guidance "When creating a module that can raise several distinct errors, a common practice is to create a base class for exceptions defined by that module, and subclass that to create specific exception classes for different error conditions: <code example>".

It may have seemed like a good idea at the time, but we now know from years of experience that this is an experiment that didn't pan out and we can consider this guidance a Now-Contraindicated Best Practice Of Yesteryearâ„¢.

Modules define subclasses of Exception for lots of reasons. Some of those subclasses have no relation to one another except that they are subclasses of Exception. Some of those subclasses define Exception instances that are never raised by code in the module, but that are expected to be raised by code passed to and called by the module.

Yes, there are times when a common base class may be appropriate - such as when an inheritance hierarchy and polymorphism that satisfy the Liskov Substitution Principle make sense for the Exception subclasses, and when the base class itself is used (such as when the base class is an item in the Raises: section of a function's doc string). But these cases aren't so common as to justify the advice being offered generally about all Exception subclass definitions.

Exception subclasses are just classes. Advising that authors may wish to define a common base class for all Exception subclasses in a module is like advising authors that they may wish to define a common base class for all object subclasses in a module - it's very, very, very occasionally a good idea in the particular circumstances of a particular module's implementation, but very generally not.
History
Date User Action Args
2018-08-28 20:40:51Nathaniel Manistasetrecipients: + Nathaniel Manista, docs@python
2018-08-28 20:40:51Nathaniel Manistasetmessageid: <1535488851.13.0.56676864532.issue34538@psf.upfronthosting.co.za>
2018-08-28 20:40:51Nathaniel Manistalinkissue34538 messages
2018-08-28 20:40:51Nathaniel Manistacreate