~dkuhlman/python-training-materials/Materials

« back to all changes in this revision

Viewing changes to python-3.5.1-docs-html/_sources/tutorial/errors.txt

  • Committer: Dave Kuhlman
  • Date: 2017-04-15 16:24:56 UTC
  • Revision ID: dkuhlman@davekuhlman.org-20170415162456-iav9vozzg4iwqwv3
Updated docs

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
.. _tut-errors:
2
 
 
3
 
*********************
4
 
Errors and Exceptions
5
 
*********************
6
 
 
7
 
Until now error messages haven't been more than mentioned, but if you have tried
8
 
out the examples you have probably seen some.  There are (at least) two
9
 
distinguishable kinds of errors: *syntax errors* and *exceptions*.
10
 
 
11
 
 
12
 
.. _tut-syntaxerrors:
13
 
 
14
 
Syntax Errors
15
 
=============
16
 
 
17
 
Syntax errors, also known as parsing errors, are perhaps the most common kind of
18
 
complaint you get while you are still learning Python::
19
 
 
20
 
   >>> while True print('Hello world')
21
 
     File "<stdin>", line 1, in ?
22
 
       while True print('Hello world')
23
 
                      ^
24
 
   SyntaxError: invalid syntax
25
 
 
26
 
The parser repeats the offending line and displays a little 'arrow' pointing at
27
 
the earliest point in the line where the error was detected.  The error is
28
 
caused by (or at least detected at) the token *preceding* the arrow: in the
29
 
example, the error is detected at the function :func:`print`, since a colon
30
 
(``':'``) is missing before it.  File name and line number are printed so you
31
 
know where to look in case the input came from a script.
32
 
 
33
 
 
34
 
.. _tut-exceptions:
35
 
 
36
 
Exceptions
37
 
==========
38
 
 
39
 
Even if a statement or expression is syntactically correct, it may cause an
40
 
error when an attempt is made to execute it. Errors detected during execution
41
 
are called *exceptions* and are not unconditionally fatal: you will soon learn
42
 
how to handle them in Python programs.  Most exceptions are not handled by
43
 
programs, however, and result in error messages as shown here::
44
 
 
45
 
   >>> 10 * (1/0)
46
 
   Traceback (most recent call last):
47
 
     File "<stdin>", line 1, in ?
48
 
   ZeroDivisionError: division by zero
49
 
   >>> 4 + spam*3
50
 
   Traceback (most recent call last):
51
 
     File "<stdin>", line 1, in ?
52
 
   NameError: name 'spam' is not defined
53
 
   >>> '2' + 2
54
 
   Traceback (most recent call last):
55
 
     File "<stdin>", line 1, in ?
56
 
   TypeError: Can't convert 'int' object to str implicitly
57
 
 
58
 
The last line of the error message indicates what happened. Exceptions come in
59
 
different types, and the type is printed as part of the message: the types in
60
 
the example are :exc:`ZeroDivisionError`, :exc:`NameError` and :exc:`TypeError`.
61
 
The string printed as the exception type is the name of the built-in exception
62
 
that occurred.  This is true for all built-in exceptions, but need not be true
63
 
for user-defined exceptions (although it is a useful convention). Standard
64
 
exception names are built-in identifiers (not reserved keywords).
65
 
 
66
 
The rest of the line provides detail based on the type of exception and what
67
 
caused it.
68
 
 
69
 
The preceding part of the error message shows the context where the exception
70
 
happened, in the form of a stack traceback. In general it contains a stack
71
 
traceback listing source lines; however, it will not display lines read from
72
 
standard input.
73
 
 
74
 
:ref:`bltin-exceptions` lists the built-in exceptions and their meanings.
75
 
 
76
 
 
77
 
.. _tut-handling:
78
 
 
79
 
Handling Exceptions
80
 
===================
81
 
 
82
 
It is possible to write programs that handle selected exceptions. Look at the
83
 
following example, which asks the user for input until a valid integer has been
84
 
entered, but allows the user to interrupt the program (using :kbd:`Control-C` or
85
 
whatever the operating system supports); note that a user-generated interruption
86
 
is signalled by raising the :exc:`KeyboardInterrupt` exception. ::
87
 
 
88
 
   >>> while True:
89
 
   ...     try:
90
 
   ...         x = int(input("Please enter a number: "))
91
 
   ...         break
92
 
   ...     except ValueError:
93
 
   ...         print("Oops!  That was no valid number.  Try again...")
94
 
   ...
95
 
 
96
 
The :keyword:`try` statement works as follows.
97
 
 
98
 
* First, the *try clause* (the statement(s) between the :keyword:`try` and
99
 
  :keyword:`except` keywords) is executed.
100
 
 
101
 
* If no exception occurs, the *except clause* is skipped and execution of the
102
 
  :keyword:`try` statement is finished.
103
 
 
104
 
* If an exception occurs during execution of the try clause, the rest of the
105
 
  clause is skipped.  Then if its type matches the exception named after the
106
 
  :keyword:`except` keyword, the except clause is executed, and then execution
107
 
  continues after the :keyword:`try` statement.
108
 
 
109
 
* If an exception occurs which does not match the exception named in the except
110
 
  clause, it is passed on to outer :keyword:`try` statements; if no handler is
111
 
  found, it is an *unhandled exception* and execution stops with a message as
112
 
  shown above.
113
 
 
114
 
A :keyword:`try` statement may have more than one except clause, to specify
115
 
handlers for different exceptions.  At most one handler will be executed.
116
 
Handlers only handle exceptions that occur in the corresponding try clause, not
117
 
in other handlers of the same :keyword:`try` statement.  An except clause may
118
 
name multiple exceptions as a parenthesized tuple, for example::
119
 
 
120
 
   ... except (RuntimeError, TypeError, NameError):
121
 
   ...     pass
122
 
 
123
 
The last except clause may omit the exception name(s), to serve as a wildcard.
124
 
Use this with extreme caution, since it is easy to mask a real programming error
125
 
in this way!  It can also be used to print an error message and then re-raise
126
 
the exception (allowing a caller to handle the exception as well)::
127
 
 
128
 
   import sys
129
 
 
130
 
   try:
131
 
       f = open('myfile.txt')
132
 
       s = f.readline()
133
 
       i = int(s.strip())
134
 
   except OSError as err:
135
 
       print("OS error: {0}".format(err))
136
 
   except ValueError:
137
 
       print("Could not convert data to an integer.")
138
 
   except:
139
 
       print("Unexpected error:", sys.exc_info()[0])
140
 
       raise
141
 
 
142
 
The :keyword:`try` ... :keyword:`except` statement has an optional *else
143
 
clause*, which, when present, must follow all except clauses.  It is useful for
144
 
code that must be executed if the try clause does not raise an exception.  For
145
 
example::
146
 
 
147
 
   for arg in sys.argv[1:]:
148
 
       try:
149
 
           f = open(arg, 'r')
150
 
       except IOError:
151
 
           print('cannot open', arg)
152
 
       else:
153
 
           print(arg, 'has', len(f.readlines()), 'lines')
154
 
           f.close()
155
 
 
156
 
The use of the :keyword:`else` clause is better than adding additional code to
157
 
the :keyword:`try` clause because it avoids accidentally catching an exception
158
 
that wasn't raised by the code being protected by the :keyword:`try` ...
159
 
:keyword:`except` statement.
160
 
 
161
 
When an exception occurs, it may have an associated value, also known as the
162
 
exception's *argument*. The presence and type of the argument depend on the
163
 
exception type.
164
 
 
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
167
 
``instance.args``.  For convenience, the exception instance defines
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. ::
171
 
 
172
 
   >>> try:
173
 
   ...    raise Exception('spam', 'eggs')
174
 
   ... except Exception as inst:
175
 
   ...    print(type(inst))    # the exception instance
176
 
   ...    print(inst.args)     # arguments stored in .args
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
180
 
   ...    print('x =', x)
181
 
   ...    print('y =', y)
182
 
   ...
183
 
   <class 'Exception'>
184
 
   ('spam', 'eggs')
185
 
   ('spam', 'eggs')
186
 
   x = spam
187
 
   y = eggs
188
 
 
189
 
If an exception has arguments, they are printed as the last part ('detail') of
190
 
the message for unhandled exceptions.
191
 
 
192
 
Exception handlers don't just handle exceptions if they occur immediately in the
193
 
try clause, but also if they occur inside functions that are called (even
194
 
indirectly) in the try clause. For example::
195
 
 
196
 
   >>> def this_fails():
197
 
   ...     x = 1/0
198
 
   ...
199
 
   >>> try:
200
 
   ...     this_fails()
201
 
   ... except ZeroDivisionError as err:
202
 
   ...     print('Handling run-time error:', err)
203
 
   ...
204
 
   Handling run-time error: int division or modulo by zero
205
 
 
206
 
 
207
 
.. _tut-raising:
208
 
 
209
 
Raising Exceptions
210
 
==================
211
 
 
212
 
The :keyword:`raise` statement allows the programmer to force a specified
213
 
exception to occur. For example::
214
 
 
215
 
   >>> raise NameError('HiThere')
216
 
   Traceback (most recent call last):
217
 
     File "<stdin>", line 1, in ?
218
 
   NameError: HiThere
219
 
 
220
 
The sole argument to :keyword:`raise` indicates the exception to be raised.
221
 
This must be either an exception instance or an exception class (a class that
222
 
derives from :class:`Exception`).
223
 
 
224
 
If you need to determine whether an exception was raised but don't intend to
225
 
handle it, a simpler form of the :keyword:`raise` statement allows you to
226
 
re-raise the exception::
227
 
 
228
 
   >>> try:
229
 
   ...     raise NameError('HiThere')
230
 
   ... except NameError:
231
 
   ...     print('An exception flew by!')
232
 
   ...     raise
233
 
   ...
234
 
   An exception flew by!
235
 
   Traceback (most recent call last):
236
 
     File "<stdin>", line 2, in ?
237
 
   NameError: HiThere
238
 
 
239
 
 
240
 
.. _tut-userexceptions:
241
 
 
242
 
User-defined Exceptions
243
 
=======================
244
 
 
245
 
Programs may name their own exceptions by creating a new exception class (see
246
 
:ref:`tut-classes` for more about Python classes).  Exceptions should typically
247
 
be derived from the :exc:`Exception` class, either directly or indirectly.  For
248
 
example::
249
 
 
250
 
   >>> class MyError(Exception):
251
 
   ...     def __init__(self, value):
252
 
   ...         self.value = value
253
 
   ...     def __str__(self):
254
 
   ...         return repr(self.value)
255
 
   ...
256
 
   >>> try:
257
 
   ...     raise MyError(2*2)
258
 
   ... except MyError as e:
259
 
   ...     print('My exception occurred, value:', e.value)
260
 
   ...
261
 
   My exception occurred, value: 4
262
 
   >>> raise MyError('oops!')
263
 
   Traceback (most recent call last):
264
 
     File "<stdin>", line 1, in ?
265
 
   __main__.MyError: 'oops!'
266
 
 
267
 
In this example, the default :meth:`__init__` of :class:`Exception` has been
268
 
overridden.  The new behavior simply creates the *value* attribute.  This
269
 
replaces the default behavior of creating the *args* attribute.
270
 
 
271
 
Exception classes can be defined which do anything any other class can do, but
272
 
are usually kept simple, often only offering a number of attributes that allow
273
 
information about the error to be extracted by handlers for the exception.  When
274
 
creating a module that can raise several distinct errors, a common practice is
275
 
to create a base class for exceptions defined by that module, and subclass that
276
 
to create specific exception classes for different error conditions::
277
 
 
278
 
   class Error(Exception):
279
 
       """Base class for exceptions in this module."""
280
 
       pass
281
 
 
282
 
   class InputError(Error):
283
 
       """Exception raised for errors in the input.
284
 
 
285
 
       Attributes:
286
 
           expression -- input expression in which the error occurred
287
 
           message -- explanation of the error
288
 
       """
289
 
 
290
 
       def __init__(self, expression, message):
291
 
           self.expression = expression
292
 
           self.message = message
293
 
 
294
 
   class TransitionError(Error):
295
 
       """Raised when an operation attempts a state transition that's not
296
 
       allowed.
297
 
 
298
 
       Attributes:
299
 
           previous -- state at beginning of transition
300
 
           next -- attempted new state
301
 
           message -- explanation of why the specific transition is not allowed
302
 
       """
303
 
 
304
 
       def __init__(self, previous, next, message):
305
 
           self.previous = previous
306
 
           self.next = next
307
 
           self.message = message
308
 
 
309
 
Most exceptions are defined with names that end in "Error," similar to the
310
 
naming of the standard exceptions.
311
 
 
312
 
Many standard modules define their own exceptions to report errors that may
313
 
occur in functions they define.  More information on classes is presented in
314
 
chapter :ref:`tut-classes`.
315
 
 
316
 
 
317
 
.. _tut-cleanup:
318
 
 
319
 
Defining Clean-up Actions
320
 
=========================
321
 
 
322
 
The :keyword:`try` statement has another optional clause which is intended to
323
 
define clean-up actions that must be executed under all circumstances.  For
324
 
example::
325
 
 
326
 
   >>> try:
327
 
   ...     raise KeyboardInterrupt
328
 
   ... finally:
329
 
   ...     print('Goodbye, world!')
330
 
   ...
331
 
   Goodbye, world!
332
 
   Traceback (most recent call last):
333
 
     File "<stdin>", line 2, in ?
334
 
   KeyboardInterrupt
335
 
 
336
 
A *finally clause* is always executed before leaving the :keyword:`try`
337
 
statement, whether an exception has occurred or not. When an exception has
338
 
occurred in the :keyword:`try` clause and has not been handled by an
339
 
:keyword:`except` clause (or it has occurred in an :keyword:`except` or
340
 
:keyword:`else` clause), it is re-raised after the :keyword:`finally` clause has
341
 
been executed.  The :keyword:`finally` clause is also executed "on the way out"
342
 
when any other clause of the :keyword:`try` statement is left via a
343
 
:keyword:`break`, :keyword:`continue` or :keyword:`return` statement.  A more
344
 
complicated example::
345
 
 
346
 
   >>> def divide(x, y):
347
 
   ...     try:
348
 
   ...         result = x / y
349
 
   ...     except ZeroDivisionError:
350
 
   ...         print("division by zero!")
351
 
   ...     else:
352
 
   ...         print("result is", result)
353
 
   ...     finally:
354
 
   ...         print("executing finally clause")
355
 
   ...
356
 
   >>> divide(2, 1)
357
 
   result is 2.0
358
 
   executing finally clause
359
 
   >>> divide(2, 0)
360
 
   division by zero!
361
 
   executing finally clause
362
 
   >>> divide("2", "1")
363
 
   executing finally clause
364
 
   Traceback (most recent call last):
365
 
     File "<stdin>", line 1, in ?
366
 
     File "<stdin>", line 3, in divide
367
 
   TypeError: unsupported operand type(s) for /: 'str' and 'str'
368
 
 
369
 
As you can see, the :keyword:`finally` clause is executed in any event.  The
370
 
:exc:`TypeError` raised by dividing two strings is not handled by the
371
 
:keyword:`except` clause and therefore re-raised after the :keyword:`finally`
372
 
clause has been executed.
373
 
 
374
 
In real world applications, the :keyword:`finally` clause is useful for
375
 
releasing external resources (such as files or network connections), regardless
376
 
of whether the use of the resource was successful.
377
 
 
378
 
 
379
 
.. _tut-cleanup-with:
380
 
 
381
 
Predefined Clean-up Actions
382
 
===========================
383
 
 
384
 
Some objects define standard clean-up actions to be undertaken when the object
385
 
is no longer needed, regardless of whether or not the operation using the object
386
 
succeeded or failed. Look at the following example, which tries to open a file
387
 
and print its contents to the screen. ::
388
 
 
389
 
   for line in open("myfile.txt"):
390
 
       print(line, end="")
391
 
 
392
 
The problem with this code is that it leaves the file open for an indeterminate
393
 
amount of time after this part of the code has finished executing.
394
 
This is not an issue in simple scripts, but can be a problem for larger
395
 
applications. The :keyword:`with` statement allows objects like files to be
396
 
used in a way that ensures they are always cleaned up promptly and correctly. ::
397
 
 
398
 
   with open("myfile.txt") as f:
399
 
       for line in f:
400
 
           print(line, end="")
401
 
 
402
 
After the statement is executed, the file *f* is always closed, even if a
403
 
problem was encountered while processing the lines. Objects which, like files,
404
 
provide predefined clean-up actions will indicate this in their documentation.
405
 
 
406