This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Document best practices for exceptions
Type: behavior Stage: needs patch
Components: Documentation Versions: Python 3.10, Python 3.9, Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: belopolsky, chris.jerdonek, docs@python, eric.araujo, ezio.melotti, hynek, iritkatriel, r.david.murray, rhettinger, terry.reedy, tshepang, veky
Priority: normal Keywords:

Created on 2012-06-08 01:22 by r.david.murray, last changed 2022-04-11 14:57 by admin.

Messages (11)
msg162513 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2012-06-08 01:22
And I wish I knew what those were.
msg162540 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2012-06-08 18:03
I don’t understand the request.
msg162541 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2012-06-08 18:08
The obvious example is that the tutorial makes no mention of calling 'super' in __init__.  I'm also aware that there are issues of pickleability that arise if you do things one way, but do not arise if you do things another way.  But I don't know the details, and I'd like to see the tutorial show an example of the *best* way to write a user defined exception so that they behave like the built in Python exceptions.
msg164033 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2012-06-26 04:33
This seems more like a Stack Overflow question or fodder for a blog post.  

When such best practices become well known and agreed upon, they can be added toa HOWTO document.  The tutorial is problematic because beginners start there and sprinkling the nuances of super() throughout the tutorial will only get in the way of the main learning points for a given section.
msg164076 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2012-06-26 13:43
OK, let's move this, then.  I asked the question because I'd like to know what the best practice is for exceptions in the stdlib.  This is an area in which we have made quite a bit of progress recently (ie: the work done on exceptions for Python3, and PEP 3151), but I think there is still a lack of documentation (and possibly consensus?) on best practices for the stdlib.
msg164434 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2012-06-30 23:08
I agree with Raymond that this, especially super() is not tutorial material. I agree with David that we need something somewhere else. Just today a commit was pushed about IOException subclasses causing problems because they have an __init__ but don't call super(). (Something like that.) I had no idea about this issue.

The stdlib has things like "class ModException(Exception): pass". Is that okay because it does *not* have __init__ override?
msg171039 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2012-09-23 14:03
I'm not sure this belongs to the devguide.
Ideally the best way to do it should be documented in the docs.
This doesn't necessary mean it should be in the tutorial, but if the way documented in the tutorial is "wrong", I'd rather tell users "this is the right way, do like this and don't worry about the details right now (but follow this link if you want to know more)" than "use this wrongish way that is simple but will break as soon as you start doing something more complex".
msg171065 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2012-09-23 17:23
I think the Library Manual's chapter 5 on exceptions, currently called Built-in Exceptions, would be a good place for a section on subclassing exceptions.
msg183034 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2013-02-26 08:51
I'm removing the "devguide" component and update the title accordingly.
msg380713 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2020-11-10 20:58
There are examples in the tutorial where super().__init__ is not called, like here: 

https://docs.python.org/3/tutorial/errors.html#user-defined-exceptions
msg380739 - (view) Author: Vedran Čačić (veky) * Date: 2020-11-11 07:31
Terry: of course, if a method is not overridden, there's no need to call super()'s method, since the only thing it would do is call the same thing that would be called without overriding.

But of course, if Exception's __init__ does anything, then super() should be called from the overrridden method.
History
Date User Action Args
2022-04-11 14:57:31adminsetgithub: 59239
2020-11-11 07:31:11vekysetnosy: + veky
messages: + msg380739
2020-11-10 20:58:21iritkatrielsetnosy: + iritkatriel

messages: + msg380713
versions: + Python 3.8, Python 3.9, Python 3.10, - Python 2.7, Python 3.2, Python 3.3
2013-02-26 08:51:19ezio.melottisetmessages: + msg183034
components: + Documentation, - Devguide
title: Devguide should document best practices for stdlib exceptions -> Document best practices for exceptions
2012-09-23 17:26:53chris.jerdoneksetnosy: + chris.jerdonek
2012-09-23 17:23:13terry.reedysetmessages: + msg171065
2012-09-23 14:03:06ezio.melottisetmessages: + msg171039
2012-06-30 23:08:09terry.reedysetnosy: + terry.reedy
messages: + msg164434
2012-06-29 18:57:58tshepangsetnosy: + tshepang
2012-06-26 13:43:24r.david.murraysetstatus: closed -> open
title: tutorial should use best practices in user defined exceptions section -> Devguide should document best practices for stdlib exceptions
messages: + msg164076

components: + Devguide, - Documentation
resolution: later ->
2012-06-26 04:33:25rhettingersetstatus: open -> closed

nosy: + rhettinger
messages: + msg164033

resolution: later
2012-06-15 08:09:59ezio.melottisetnosy: + ezio.melotti
2012-06-08 18:40:40belopolskysetnosy: + belopolsky
2012-06-08 18:40:09belopolskysettitle: tutorial should use best practices in user defined execeptions section -> tutorial should use best practices in user defined exceptions section
2012-06-08 18:08:51r.david.murraysetmessages: + msg162541
2012-06-08 18:03:26eric.araujosetnosy: + eric.araujo
messages: + msg162540
2012-06-08 08:50:03hyneksetnosy: + hynek
2012-06-08 01:22:04r.david.murraycreate