Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f5f2630

Browse files
committed
#3525: 3.0 exception changes in tutorial.
1 parent 1e3830a commit f5f2630

2 files changed

Lines changed: 16 additions & 20 deletions

File tree

Doc/tutorial/classes.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ class or a base class thereof (but not the other way around --- an except clause
671671
listing a derived class is not compatible with a base class). For example, the
672672
following code will print B, C, D in that order::
673673

674-
class B:
674+
class B(Exception):
675675
pass
676676
class C(B):
677677
pass

Doc/tutorial/errors.rst

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
. _tut-errors:
1+
.. _tut-errors:
22

33
*********************
44
Errors and Exceptions
@@ -131,8 +131,8 @@ the exception (allowing a caller to handle the exception as well)::
131131
f = open('myfile.txt')
132132
s = f.readline()
133133
i = int(s.strip())
134-
except IOError as (errno, strerror):
135-
print("I/O error({0}): {1}".format(errno, strerror))
134+
except IOError as err:
135+
print("I/O error: {0}".format(err))
136136
except ValueError:
137137
print("Could not convert data to an integer.")
138138
except:
@@ -162,25 +162,21 @@ When an exception occurs, it may have an associated value, also known as the
162162
exception's *argument*. The presence and type of the argument depend on the
163163
exception type.
164164

165-
The except clause may specify a variable after the exception name (or tuple).
166-
The variable is bound to an exception instance with the arguments stored in
165+
The except clause may specify a variable after the exception name. The
166+
variable is bound to an exception instance with the arguments stored in
167167
``instance.args``. For convenience, the exception instance defines
168-
:meth:`__getitem__` and :meth:`__str__` so the arguments can be accessed or
169-
printed directly without having to reference ``.args``.
170-
171-
But use of ``.args`` is discouraged. Instead, the preferred use is to pass a
172-
single argument to an exception (which can be a tuple if multiple arguments are
173-
needed) and have it bound to the ``message`` attribute. One may also
174-
instantiate an exception first before raising it and add any attributes to it as
175-
desired. ::
168+
:meth:`__str__` so the arguments can be printed directly without having to
169+
reference ``.args``. One may also instantiate an exception first before
170+
raising it and add any attributes to it as desired. ::
176171

177172
>>> try:
178173
... raise Exception('spam', 'eggs')
179174
... except Exception as inst:
180175
... print(type(inst)) # the exception instance
181176
... print(inst.args) # arguments stored in .args
182-
... print(inst) # __str__ allows args to be printed directly
183-
... x, y = inst # __getitem__ allows args to be unpacked directly
177+
... print(inst) # __str__ allows args to be printed directly,
178+
... # but may be overridden in exception subclasses
179+
... x, y = inst.args # unpack args
184180
... print('x =', x)
185181
... print('y =', y)
186182
...
@@ -190,7 +186,7 @@ desired. ::
190186
x = spam
191187
y = eggs
192188

193-
If an exception has an argument, it is printed as the last part ('detail') of
189+
If an exception has arguments, they are printed as the last part ('detail') of
194190
the message for unhandled exceptions.
195191

196192
Exception handlers don't just handle exceptions if they occur immediately in the
@@ -202,10 +198,10 @@ indirectly) in the try clause. For example::
202198
...
203199
>>> try:
204200
... this_fails()
205-
... except ZeroDivisionError as detail:
206-
... print('Handling run-time error:', detail)
201+
... except ZeroDivisionError as err:
202+
... print('Handling run-time error:', err)
207203
...
208-
Handling run-time error: integer division or modulo by zero
204+
Handling run-time error: int division or modulo by zero
209205

210206

211207
.. _tut-raising:

0 commit comments

Comments
 (0)