classification
Title: Minor improvements to the "Handling Exceptions" part of the tutorial
Type: behavior Stage: resolved
Components: Documentation Versions: Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: docs@python, eric.araujo, marienz, python-dev, r.david.murray, terry.reedy
Priority: normal Keywords: patch

Created on 2010-05-07 17:25 by marienz, last changed 2012-06-08 01:51 by r.david.murray. This issue is now closed.

Files
File name Uploaded Description Edit
errors.patch marienz, 2010-09-12 11:23 Preliminary patch.
Messages (7)
msg105218 - (view) Author: Marien Zwart (marienz) * Date: 2010-05-07 17:25
Based on questions asked on freenode's #python by people reading the tutorial I would like to suggest two improvements to http://docs.python.org/tutorial/errors.html#handling-exceptions :

- Mention the older "except SomeException, e:" syntax too, not just "except SomeException as e:". I have had people ask me to help them upgrade to a newer python because they thought their version of python did not support catching exception instances (their distro python being 2.5). A big fat warning that they're reading the Python 2.6 tutorial (with links to older tutorials) may also work.

- Mention "except IOError as e" before mentioning "except IOError as (errno, strerror):". The latter is an advanced and relatively unusual trick, combining regular exception catching and unpacking. Those two concepts need to be explained before the combination is. I have had people ask me how to do "except KeyError as (key, insert_something_here):" because they thought they *always* needed those parens there.

(perhaps just not mentioning unpacking here at all, using "except IOError as e:" and then using e.errno would make more sense, especially since "except IOError as (errno, strerror):" is a SyntaxError in python 3 and up (because of the unpacking) and python 2.5.x and down (because of the "as")...)

I can try to write a patch to the documentation if you like.
msg105746 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-05-14 19:28
The older exception catching syntax should not be promoted but could be mentioned, I agree. Adding a paragraph explaining why it was changed would do the trick.

Regarding the non-obviousness of which Python version the docs apply to, it is indeed mentioned on the front page of the docs, and repeated in the top-left corner of each page. You could propose making it bigger. Another way of doing this would be to have the page redirect to a URI that includes the version. That way, there would be another place to look for the version, and it would also make it easier to find out how to find the doc for other versions.

Last, exception unpacking being deprecated (because it’s horrible) and causing pain to newcomers seems a good reason to shot it down. Perhaps we should leave a short note about it, for people that get strange errors when using it inadvertently (or not getting an error where they thought they would).

I suggest you wait some more days for feedback from people with more experience with the docs. Sorry if my English was a bit convoluted :)
msg114845 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-08-24 22:52
Since there has been no feedback, you could write a patch to get the discussion going.
msg116172 - (view) Author: Marien Zwart (marienz) * Date: 2010-09-12 11:23
Sorry for forgetting about this for so long.

The attached patch makes the following changes:

Removes the two instances I noticed of treating exception instances as sequences. This no longer works in python 3 and is not very useful in python 2, so best avoided.

Adds a warning explaining ``except FooException, BarException:`` does not do what you might think. I think this is a good warning to have (because you *can* omit those parens in many other places) and it mentions the pre-python 2.6 syntax for "as" by side effect.

There are two things I'm not entirely happy about:

I initially wrote ``print "I/O error({0.errno}): {0.strerror}".format(e)``, but then noticed this string formatting trick is not mentioned in the previous chapter of the tutorial. Dict access (``"{0[foo]}".format(d)``) is, but attribute access is not. Is this worth adding to that chapter, so it can be used here?

Binding an exception instance to a variable (using "as") is explained near the bottom of the section on catching exceptions, well before it is used. Perhaps this could do with a bit of reordering? I felt it better to keep my initial patch more minimal though.

Comments? :)
msg128243 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2011-02-09 22:21
> Removes the two instances I noticed of treating exception instances as sequences.
Looks good.

> Adds a warning explaining ``except FooException, BarException:`` does not do what you might think.
Ditto.

> There are two things I'm not entirely happy about:

> I initially wrote ``print "I/O error({0.errno}): {0.strerror}".format(e)``,
> but then noticed this string formatting trick is not mentioned in the previous chapter
> of the tutorial. Dict access (``"{0[foo]}".format(d)``) is, but attribute access
> is not. Is this worth adding to that chapter, so it can be used here?
It’s not at all a trick :)  Given that item access is already covered, I don’t think your use of attribute access would make the example harder for beginners, but I may not be the best judge.

> Binding an exception instance to a variable (using "as") is explained near the bottom
> of the section on catching exceptions, well before it is used.
> Perhaps this could do with a bit of reordering?
+1.

A note about workflow: patches should usually apply to the py3k branch, the person who commits takes care of backporting to 3.1 and 2.7.  In this case, some of your editions are not relevant for 3.x, so I suggest you use one of those workflows:
1) Make one patch for py3k and one for 2.7
2) Make one for 2.7, with comments indicating parts that apply to py3k too
3) Make one patch with edits relevant for py3k and 2.7, and another one for 2.7-only edits
msg162515 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2012-06-08 01:47
New changeset b873afe640e2 by R David Murray in branch '2.7':
#8652: update errors tutorial.
http://hg.python.org/cpython/rev/b873afe640e2
msg162516 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2012-06-08 01:51
The Python3 tutorial was already fixed, and the explanation of the parens is not needed there since the "old syntax" is not supported.

I did not do any reordering since the use of 'as' immediately follows in the text and seems to make fine sense as is.  Actually, it now makes more sense than the equivalent exposition in the Python3 docs, where the paragraph is absent.

Thanks for the patch, Marien.
History
Date User Action Args
2012-06-08 01:51:54r.david.murraysetstatus: open -> closed

type: behavior
versions: - Python 3.1, Python 3.2, Python 3.3
nosy: + r.david.murray

messages: + msg162516
resolution: fixed
stage: patch review -> resolved
2012-06-08 01:47:09python-devsetnosy: + python-dev
messages: + msg162515
2011-02-09 22:21:42eric.araujosetversions: + Python 3.1, Python 2.7, Python 3.2, Python 3.3
nosy: + terry.reedy

messages: + msg128243

stage: patch review
2010-09-12 11:24:01marienzsetfiles: + errors.patch
keywords: + patch
messages: + msg116172
2010-08-24 22:52:07eric.araujosetmessages: + msg114845
2010-05-14 19:28:10eric.araujosetnosy: + eric.araujo
messages: + msg105746
2010-05-07 17:25:32marienzcreate