summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorR David Murray <rdmurray@bitdance.com>2012-06-08 01:46:44 (GMT)
committerR David Murray <rdmurray@bitdance.com>2012-06-08 01:46:44 (GMT)
commit1eda5c9a83b2fc2c37c25ebde93155767405637d (patch)
treebb371016b93a45055a36dc0cabc8187b13b85adb
parent5525eb77fa9a529214b817395677676fb87dfc21 (diff)
downloadcpython-1eda5c9a83b2fc2c37c25ebde93155767405637d.zip
cpython-1eda5c9a83b2fc2c37c25ebde93155767405637d.tar.gz
cpython-1eda5c9a83b2fc2c37c25ebde93155767405637d.tar.bz2
#8652: update errors tutorial.
The tutorial had some outdated examples. The patch also adds a caution about the meaning of parens in the except statement. Patch by Marien Zwart.
-rw-r--r--Doc/tutorial/errors.rst14
1 files changed, 11 insertions, 3 deletions
diff --git a/Doc/tutorial/errors.rst b/Doc/tutorial/errors.rst
index 1351957..5fc1eeb 100644
--- a/Doc/tutorial/errors.rst
+++ b/Doc/tutorial/errors.rst
@@ -120,6 +120,14 @@ name multiple exceptions as a parenthesized tuple, for example::
... except (RuntimeError, TypeError, NameError):
... pass
+Note that the parentheses around this tuple are required, because
+``except ValueError, e:`` was the syntax used for what is normally
+written as ``except ValueError as e:`` in modern Python (described
+below). The old syntax is still supported for backwards compatibility.
+This means ``except RuntimeError, TypeError`` is not equivalent to
+``except (RuntimeError, TypeError):`` but to ``except RuntimeError as
+TypeError:`` which is not what you want.
+
The last except clause may omit the exception name(s), to serve as a wildcard.
Use this with extreme caution, since it is easy to mask a real programming error
in this way! It can also be used to print an error message and then re-raise
@@ -131,8 +139,8 @@ the exception (allowing a caller to handle the exception as well)::
f = open('myfile.txt')
s = f.readline()
i = int(s.strip())
- except IOError as (errno, strerror):
- print "I/O error({0}): {1}".format(errno, strerror)
+ except IOError as e:
+ print "I/O error({0}): {1}".format(e.errno, e.strerror)
except ValueError:
print "Could not convert data to an integer."
except:
@@ -177,7 +185,7 @@ attributes to it as desired. ::
... print type(inst) # the exception instance
... print inst.args # arguments stored in .args
... print inst # __str__ allows args to printed directly
- ... x, y = inst # __getitem__ allows args to be unpacked directly
+ ... x, y = inst.args
... print 'x =', x
... print 'y =', y
...