1- . _tut-errors:
1+ .. _tut-errors :
22
33*********************
44Errors 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
162162exception's *argument *. The presence and type of the argument depend on the
163163exception 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
194190the message for unhandled exceptions.
195191
196192Exception 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