1 \section{Built-in Exceptions
}
3 \declaremodule{standard
}{exceptions
}
4 \modulesynopsis{Standard exception classes.
}
7 Exceptions should be class objects.
8 The exceptions are defined in the module
\module{exceptions
}. This
9 module never needs to be imported explicitly: the exceptions are
10 provided in the built-in namespace as well as the
\module{exceptions
}
14 In past versions of Python string exceptions were supported. In
15 Python
1.5 and newer versions, all standard exceptions have been
16 converted to class objects and users are encouraged to do the same.
17 String exceptions will raise a
\code{PendingDeprecationWarning
}.
18 In future versions, support for string exceptions will be removed.
20 Two distinct string objects with the same value are considered different
21 exceptions. This is done to force programmers to use exception names
22 rather than their string value when specifying exception handlers.
23 The string value of all built-in exceptions is their name, but this is
24 not a requirement for user-defined exceptions or exceptions defined by
28 For class exceptions, in a
\keyword{try
}\stindex{try
} statement with
29 an
\keyword{except
}\stindex{except
} clause that mentions a particular
30 class, that clause also handles any exception classes derived from
31 that class (but not exception classes from which
\emph{it
} is
32 derived). Two exception classes that are not related via subclassing
33 are never equivalent, even if they have the same name.
35 The built-in exceptions listed below can be generated by the
36 interpreter or built-in functions. Except where mentioned, they have
37 an ``associated value'' indicating the detailed cause of the error.
38 This may be a string or a tuple containing several items of
39 information (e.g., an error code and a string explaining the code).
40 The associated value is the second argument to the
41 \keyword{raise
}\stindex{raise
} statement. For string exceptions, the
42 associated value itself will be stored in the variable named as the
43 second argument of the
\keyword{except
} clause (if any). For class
44 exceptions, that variable receives the exception instance. If the
45 exception class is derived from the standard root class
46 \exception{Exception
}, the associated value is present as the
47 exception instance's
\member{args
} attribute, and possibly on other
50 User code can raise built-in exceptions. This can be used to test an
51 exception handler or to
report an error condition ``just like'' the
52 situation in which the interpreter raises the same exception; but
53 beware that there is nothing to prevent user code from raising an
56 The built-in exception classes can be sub-classed to define new
57 exceptions; programmers are encouraged to at least derive new
58 exceptions from the
\exception{Exception
} base class. More
59 information on defining exceptions is available in the
60 \citetitle[../tut/tut.html
]{Python Tutorial
} under the heading
61 ``User-defined Exceptions.''
63 \setindexsubitem{(built-in exception base class)
}
65 The following exceptions are only used as base classes for other
68 \begin{excdesc
}{Exception
}
69 The root class for exceptions. All built-in exceptions are derived
70 from this class. All user-defined exceptions should also be derived
71 from this class, but this is not (yet) enforced. The
\function{str()
}
72 function, when applied to an instance of this class (or most derived
73 classes) returns the string value of the argument or arguments, or an
74 empty string if no arguments were given to the constructor. When used
75 as a sequence, this accesses the arguments given to the constructor
76 (handy for backward compatibility with old code). The arguments are
77 also available on the instance's
\member{args
} attribute, as a tuple.
80 \begin{excdesc
}{StandardError
}
81 The base class for all built-in exceptions except
82 \exception{StopIteration
} and
\exception{SystemExit
}.
83 \exception{StandardError
} itself is derived from the root class
84 \exception{Exception
}.
87 \begin{excdesc
}{ArithmeticError
}
88 The base class for those built-in exceptions that are raised for
89 various arithmetic errors:
\exception{OverflowError
},
90 \exception{ZeroDivisionError
},
\exception{FloatingPointError
}.
93 \begin{excdesc
}{LookupError
}
94 The base class for the exceptions that are raised when a key or
95 index used on a mapping or sequence is invalid:
\exception{IndexError
},
96 \exception{KeyError
}. This can be raised directly by
97 \function{sys.setdefaultencoding()
}.
100 \begin{excdesc
}{EnvironmentError
}
101 The base class for exceptions that
102 can occur outside the Python system:
\exception{IOError
},
103 \exception{OSError
}. When exceptions of this type are created with a
104 2-tuple, the first item is available on the instance's
\member{errno
}
105 attribute (it is assumed to be an error number), and the second item
106 is available on the
\member{strerror
} attribute (it is usually the
107 associated error message). The tuple itself is also available on the
108 \member{args
} attribute.
111 When an
\exception{EnvironmentError
} exception is instantiated with a
112 3-tuple, the first two items are available as above, while the third
113 item is available on the
\member{filename
} attribute. However, for
114 backwards compatibility, the
\member{args
} attribute contains only a
115 2-tuple of the first two constructor arguments.
117 The
\member{filename
} attribute is
\code{None
} when this exception is
118 created with other than
3 arguments. The
\member{errno
} and
119 \member{strerror
} attributes are also
\code{None
} when the instance was
120 created with other than
2 or
3 arguments. In this last case,
121 \member{args
} contains the verbatim constructor arguments as a tuple.
125 \setindexsubitem{(built-in exception)
}
127 The following exceptions are the exceptions that are actually raised.
129 \begin{excdesc
}{AssertionError
}
131 Raised when an
\keyword{assert
} statement fails.
134 \begin{excdesc
}{AttributeError
}
135 % xref to attribute reference?
136 Raised when an attribute reference or assignment fails. (When an
137 object does not support attribute references or attribute assignments
138 at all,
\exception{TypeError
} is raised.)
141 \begin{excdesc
}{EOFError
}
143 Raised when one of the built-in functions (
\function{input()
} or
144 \function{raw_input()
}) hits an end-of-file condition (
\EOF) without
147 (N.B.: the
\method{read()
} and
\method{readline()
} methods of file
148 objects return an empty string when they hit
\EOF.)
151 \begin{excdesc
}{FloatingPointError
}
152 Raised when a floating point operation fails. This exception is
153 always defined, but can only be raised when Python is configured
154 with the
\longprogramopt{with-fpectl
} option, or the
155 \constant{WANT_SIGFPE_HANDLER
} symbol is defined in the
156 \file{pyconfig.h
} file.
159 \begin{excdesc
}{IOError
}
161 Raised when an I/O operation (such as a
\keyword{print
} statement,
162 the built-in
\function{open()
} function or a method of a file
163 object) fails for an I/O-related reason, e.g., ``file not found'' or
166 This class is derived from
\exception{EnvironmentError
}. See the
167 discussion above for more information on exception instance
171 \begin{excdesc
}{ImportError
}
172 % XXXJH xref to import statement?
173 Raised when an
\keyword{import
} statement fails to find the module
174 definition or when a
\code{from
\textrm{\ldots} import
} fails to find a
175 name that is to be imported.
178 \begin{excdesc
}{IndexError
}
179 % XXXJH xref to sequences
180 Raised when a sequence subscript is out of range. (Slice indices are
181 silently truncated to fall in the allowed range; if an index is not a
182 plain integer,
\exception{TypeError
} is raised.)
185 \begin{excdesc
}{KeyError
}
186 % XXXJH xref to mapping objects?
187 Raised when a mapping (dictionary) key is not found in the set of
191 \begin{excdesc
}{KeyboardInterrupt
}
192 Raised when the user hits the interrupt key (normally
193 \kbd{Control-C
} or
\kbd{Delete
}). During execution, a check for
194 interrupts is made regularly.
196 Interrupts typed when a built-in function
\function{input()
} or
197 \function{raw_input()
} is waiting for input also raise this
201 \begin{excdesc
}{MemoryError
}
202 Raised when an operation runs out of memory but the situation may
203 still be rescued (by deleting some objects). The associated value is
204 a string indicating what kind of (internal) operation ran out of memory.
205 Note that because of the underlying memory management architecture
206 (C's
\cfunction{malloc()
} function), the interpreter may not
207 always be able to completely recover from this situation; it
208 nevertheless raises an exception so that a stack traceback can be
209 printed, in case a run-away program was the cause.
212 \begin{excdesc
}{NameError
}
213 Raised when a local or global name is not found. This applies only
214 to unqualified names. The associated value is an error message that
215 includes the name that could not be found.
218 \begin{excdesc
}{NotImplementedError
}
219 This exception is derived from
\exception{RuntimeError
}. In user
220 defined base classes, abstract methods should raise this exception
221 when they require derived classes to override the method.
225 \begin{excdesc
}{OSError
}
227 This class is derived from
\exception{EnvironmentError
} and is used
228 primarily as the
\refmodule{os
} module's
\code{os.error
} exception.
229 See
\exception{EnvironmentError
} above for a description of the
230 possible associated values.
234 \begin{excdesc
}{OverflowError
}
235 % XXXJH reference to long's and/or int's?
236 Raised when the result of an arithmetic operation is too large to be
237 represented. This cannot occur for long integers (which would rather
238 raise
\exception{MemoryError
} than give up). Because of the lack of
239 standardization of floating point exception handling in C, most
240 floating point operations also aren't checked. For plain integers,
241 all operations that can overflow are checked except left shift, where
242 typical applications prefer to drop bits than raise an exception.
245 \begin{excdesc
}{ReferenceError
}
246 This exception is raised when a weak reference proxy, created by the
247 \function{\refmodule{weakref
}.proxy()
} function, is used to access
248 an attribute of the referent after it has been garbage collected.
249 For more information on weak references, see the
\refmodule{weakref
}
251 \versionadded[Previously known as the
252 \exception{\refmodule{weakref
}.ReferenceError
}
256 \begin{excdesc
}{RuntimeError
}
257 Raised when an error is detected that doesn't fall in any of the
258 other categories. The associated value is a string indicating what
259 precisely went wrong. (This exception is mostly a relic from a
260 previous version of the interpreter; it is not used very much any
264 \begin{excdesc
}{StopIteration
}
265 Raised by an iterator's
\method{next()
} method to signal that there
266 are no further values.
267 This is derived from
\exception{Exception
} rather than
268 \exception{StandardError
}, since this is not considered an error in
269 its normal application.
273 \begin{excdesc
}{SyntaxError
}
274 % XXXJH xref to these functions?
275 Raised when the parser encounters a syntax error. This may occur in
276 an
\keyword{import
} statement, in an
\keyword{exec
} statement, in a call
277 to the built-in function
\function{eval()
} or
\function{input()
}, or
278 when reading the initial script or standard input (also
281 Instances of this class have atttributes
\member{filename
},
282 \member{lineno
},
\member{offset
} and
\member{text
} for easier access
283 to the details.
\function{str()
} of the exception instance returns
287 \begin{excdesc
}{SystemError
}
288 Raised when the interpreter finds an internal error, but the
289 situation does not look so serious to cause it to abandon all hope.
290 The associated value is a string indicating what went wrong (in
293 You should
report this to the author or maintainer of your Python
294 interpreter. Be sure to
report the version of the Python
295 interpreter (
\code{sys.version
}; it is also printed at the start of an
296 interactive Python session), the exact error message (the exception's
297 associated value) and if possible the source of the program that
301 \begin{excdesc
}{SystemExit
}
302 % XXXJH xref to module sys?
303 This exception is raised by the
\function{sys.exit()
} function. When it
304 is not handled, the Python interpreter exits; no stack traceback is
305 printed. If the associated value is a plain integer, it specifies the
306 system exit status (passed to C's
\cfunction{exit()
} function); if it is
307 \code{None
}, the exit status is zero; if it has another type (such as
308 a string), the object's value is printed and the exit status is one.
310 Instances have an attribute
\member{code
} which is set to the
311 proposed exit status or error message (defaulting to
\code{None
}).
312 Also, this exception derives directly from
\exception{Exception
} and
313 not
\exception{StandardError
}, since it is not technically an error.
315 A call to
\function{sys.exit()
} is translated into an exception so that
316 clean-up handlers (
\keyword{finally
} clauses of
\keyword{try
} statements)
317 can be executed, and so that a debugger can execute a script without
318 running the risk of losing control. The
\function{os._exit()
} function
319 can be used if it is absolutely positively necessary to exit
320 immediately (for example, in the child process after a call to
324 \begin{excdesc
}{TypeError
}
325 Raised when a built-in operation or function is applied to an object
326 of inappropriate type. The associated value is a string giving
327 details about the type mismatch.
330 \begin{excdesc
}{UnboundLocalError
}
331 Raised when a reference is made to a local variable in a function or
332 method, but no value has been bound to that variable. This is a
333 subclass of
\exception{NameError
}.
337 \begin{excdesc
}{UnicodeError
}
338 Raised when a Unicode-related encoding or decoding error occurs. It
339 is a subclass of
\exception{ValueError
}.
343 \begin{excdesc
}{UnicodeEncodeError
}
344 Raised when a Unicode-related error occurs during encoding. It
345 is a subclass of
\exception{UnicodeError
}.
349 \begin{excdesc
}{UnicodeDecodeError
}
350 Raised when a Unicode-related error occurs during decoding. It
351 is a subclass of
\exception{UnicodeError
}.
355 \begin{excdesc
}{UnicodeTranslateError
}
356 Raised when a Unicode-related error occurs during translating. It
357 is a subclass of
\exception{UnicodeError
}.
361 \begin{excdesc
}{ValueError
}
362 Raised when a built-in operation or function receives an argument
363 that has the right type but an inappropriate value, and the
364 situation is not described by a more precise exception such as
365 \exception{IndexError
}.
368 \begin{excdesc
}{WindowsError
}
369 Raised when a Windows-specific error occurs or when the error number
370 does not correspond to an
\cdata{errno
} value. The
371 \member{errno
} and
\member{strerror
} values are created from the
372 return values of the
\cfunction{GetLastError()
} and
373 \cfunction{FormatMessage()
} functions from the Windows Platform API.
374 This is a subclass of
\exception{OSError
}.
378 \begin{excdesc
}{ZeroDivisionError
}
379 Raised when the second argument of a division or modulo operation is
380 zero. The associated value is a string indicating the type of the
381 operands and the operation.
385 \setindexsubitem{(built-in warning)
}
387 The following exceptions are used as warning categories; see the
388 \refmodule{warnings
} module for more information.
390 \begin{excdesc
}{Warning
}
391 Base class for warning categories.
394 \begin{excdesc
}{UserWarning
}
395 Base class for warnings generated by user code.
398 \begin{excdesc
}{DeprecationWarning
}
399 Base class for warnings about deprecated features.
402 \begin{excdesc
}{PendingDeprecationWarning
}
403 Base class for warnings about features which will be deprecated in the future.
406 \begin{excdesc
}{SyntaxWarning
}
407 Base class for warnings about dubious syntax
410 \begin{excdesc
}{RuntimeWarning
}
411 Base class for warnings about dubious runtime behavior.
414 \begin{excdesc
}{FutureWarning
}
415 Base class for warnings about constructs that will change semantically
419 The class hierarchy for built-in exceptions is:
426 | +-- KeyboardInterrupt
428 | +-- EnvironmentError
434 | | +-- NotImplementedError
436 | | +-- UnboundLocalError
439 | | +-- IndentationError
446 | +-- ArithmeticError
447 | | +-- OverflowError
448 | | +-- ZeroDivisionError
449 | | +-- FloatingPointError
452 | | +-- UnicodeEncodeError
453 | | +-- UnicodeDecodeError
454 | | +-- UnicodeTranslateError
460 +-- DeprecationWarning
461 +-- PendingDeprecationWarning