blob: 55710863ee1a7035f91d9dc70407c0c567e0a6e6 [file] [log] [blame]
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001\section{Built-in Exceptions}
Fred Drake3bd9ab01998-07-23 19:33:08 +00002
Fred Drakeffbe6871999-04-22 21:23:22 +00003\declaremodule{standard}{exceptions}
Fred Drake3bd9ab01998-07-23 19:33:08 +00004\modulesynopsis{Standard exceptions classes.}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00005
Fred Drakeb91e9341998-07-23 17:59:49 +00006
Fred Drake7acb2182000-09-09 03:28:00 +00007Exceptions can be class objects or string objects. Though most
8exceptions have been string objects in past versions of Python, in
9Python 1.5 and newer versions, all standard exceptions have been
10converted to class objects, and users are encouraged to do the same.
11The exceptions are defined in the module \module{exceptions}. This
12module never needs to be imported explicitly: the exceptions are
Fred Drakec6920552001-09-21 21:12:30 +000013provided in the built-in namespace as well as the \module{exceptions}
14module.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000015
Guido van Rossumdf3dba01997-10-05 18:51:26 +000016Two distinct string objects with the same value are considered different
17exceptions. This is done to force programmers to use exception names
18rather than their string value when specifying exception handlers.
19The string value of all built-in exceptions is their name, but this is
20not a requirement for user-defined exceptions or exceptions defined by
21library modules.
22
Fred Drake38e5d272000-04-03 20:13:55 +000023For class exceptions, in a \keyword{try}\stindex{try} statement with
24an \keyword{except}\stindex{except} clause that mentions a particular
25class, that clause also handles any exception classes derived from
26that class (but not exception classes from which \emph{it} is
27derived). Two exception classes that are not related via subclassing
28are never equivalent, even if they have the same name.
Guido van Rossumdf3dba01997-10-05 18:51:26 +000029
30The built-in exceptions listed below can be generated by the
31interpreter or built-in functions. Except where mentioned, they have
32an ``associated value'' indicating the detailed cause of the error.
33This may be a string or a tuple containing several items of
34information (e.g., an error code and a string explaining the code).
Fred Drake38e5d272000-04-03 20:13:55 +000035The associated value is the second argument to the
36\keyword{raise}\stindex{raise} statement. For string exceptions, the
37associated value itself will be stored in the variable named as the
38second argument of the \keyword{except} clause (if any). For class
39exceptions, that variable receives the exception instance. If the
40exception class is derived from the standard root class
41\exception{Exception}, the associated value is present as the
42exception instance's \member{args} attribute, and possibly on other
43attributes as well.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000044
45User code can raise built-in exceptions. This can be used to test an
Guido van Rossumdf3dba01997-10-05 18:51:26 +000046exception handler or to report an error condition ``just like'' the
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000047situation in which the interpreter raises the same exception; but
48beware that there is nothing to prevent user code from raising an
49inappropriate error.
50
Fred Drakec6920552001-09-21 21:12:30 +000051The built-in exception classes can be sub-classed to define new
52exceptions; programmers are encouraged to at least derive new
53exceptions from the \exception{Exception} base class. More
54information on defining exceptions is available in the
55\citetitle[../tut/tut.html]{Python Tutorial} under the heading
56``User-defined Exceptions.''
57
Fred Drake19479911998-02-13 06:58:54 +000058\setindexsubitem{(built-in exception base class)}
Guido van Rossumdf3dba01997-10-05 18:51:26 +000059
60The following exceptions are only used as base classes for other
Fred Drake5828ad62000-04-06 15:03:01 +000061exceptions.
Guido van Rossumdf3dba01997-10-05 18:51:26 +000062
63\begin{excdesc}{Exception}
64The root class for exceptions. All built-in exceptions are derived
65from this class. All user-defined exceptions should also be derived
Fred Drake27467e41998-07-23 19:47:41 +000066from this class, but this is not (yet) enforced. The \function{str()}
Guido van Rossumdf3dba01997-10-05 18:51:26 +000067function, when applied to an instance of this class (or most derived
68classes) returns the string value of the argument or arguments, or an
Guido van Rossum6cd7ecb1997-10-07 14:41:04 +000069empty string if no arguments were given to the constructor. When used
70as a sequence, this accesses the arguments given to the constructor
Barry Warsawda00c871998-07-23 19:57:35 +000071(handy for backward compatibility with old code). The arguments are
Fred Drakec457ca71998-07-23 20:31:53 +000072also available on the instance's \member{args} attribute, as a tuple.
Guido van Rossumdf3dba01997-10-05 18:51:26 +000073\end{excdesc}
74
75\begin{excdesc}{StandardError}
Barry Warsawf2b45541999-02-24 00:27:14 +000076The base class for all built-in exceptions except
Fred Drakec046e972001-07-23 19:19:39 +000077\exception{StopIteration} and \exception{SystemExit}.
78\exception{StandardError} itself is derived from the root class
Fred Drake27467e41998-07-23 19:47:41 +000079\exception{Exception}.
Guido van Rossumdf3dba01997-10-05 18:51:26 +000080\end{excdesc}
81
82\begin{excdesc}{ArithmeticError}
83The base class for those built-in exceptions that are raised for
Fred Drake27467e41998-07-23 19:47:41 +000084various arithmetic errors: \exception{OverflowError},
85\exception{ZeroDivisionError}, \exception{FloatingPointError}.
Guido van Rossumdf3dba01997-10-05 18:51:26 +000086\end{excdesc}
87
88\begin{excdesc}{LookupError}
Barry Warsawda00c871998-07-23 19:57:35 +000089The base class for the exceptions that are raised when a key or
Fred Drake27467e41998-07-23 19:47:41 +000090index used on a mapping or sequence is invalid: \exception{IndexError},
Fred Drake53143be2000-10-25 21:05:29 +000091\exception{KeyError}. This can be raised directly by
92\function{sys.setdefaultencoding()}.
Guido van Rossumdf3dba01997-10-05 18:51:26 +000093\end{excdesc}
94
Barry Warsawda00c871998-07-23 19:57:35 +000095\begin{excdesc}{EnvironmentError}
96The base class for exceptions that
97can occur outside the Python system: \exception{IOError},
98\exception{OSError}. When exceptions of this type are created with a
992-tuple, the first item is available on the instance's \member{errno}
100attribute (it is assumed to be an error number), and the second item
101is available on the \member{strerror} attribute (it is usually the
102associated error message). The tuple itself is also available on the
103\member{args} attribute.
Fred Draked0bceee1999-02-02 18:00:40 +0000104\versionadded{1.5.2}
Barry Warsawda00c871998-07-23 19:57:35 +0000105
106When an \exception{EnvironmentError} exception is instantiated with a
1073-tuple, the first two items are available as above, while the third
108item is available on the \member{filename} attribute. However, for
109backwards compatibility, the \member{args} attribute contains only a
1102-tuple of the first two constructor arguments.
111
112The \member{filename} attribute is \code{None} when this exception is
113created with other than 3 arguments. The \member{errno} and
114\member{strerror} attributes are also \code{None} when the instance was
115created with other than 2 or 3 arguments. In this last case,
116\member{args} contains the verbatim constructor arguments as a tuple.
117\end{excdesc}
118
Fred Drake88c023b2000-09-07 16:33:32 +0000119
Fred Drake19479911998-02-13 06:58:54 +0000120\setindexsubitem{(built-in exception)}
Guido van Rossumdf3dba01997-10-05 18:51:26 +0000121
122The following exceptions are the exceptions that are actually raised.
Guido van Rossumdf3dba01997-10-05 18:51:26 +0000123
124\begin{excdesc}{AssertionError}
Guido van Rossumdf3dba01997-10-05 18:51:26 +0000125\stindex{assert}
Fred Drake38e5d272000-04-03 20:13:55 +0000126Raised when an \keyword{assert} statement fails.
Guido van Rossumdf3dba01997-10-05 18:51:26 +0000127\end{excdesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000128
129\begin{excdesc}{AttributeError}
130% xref to attribute reference?
131 Raised when an attribute reference or assignment fails. (When an
Guido van Rossum470be141995-03-17 16:07:09 +0000132 object does not support attribute references or attribute assignments
Fred Drake27467e41998-07-23 19:47:41 +0000133 at all, \exception{TypeError} is raised.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000134\end{excdesc}
135
136\begin{excdesc}{EOFError}
137% XXXJH xrefs here
Fred Drake27467e41998-07-23 19:47:41 +0000138 Raised when one of the built-in functions (\function{input()} or
139 \function{raw_input()}) hits an end-of-file condition (\EOF{}) without
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000140 reading any data.
141% XXXJH xrefs here
Fred Drake27467e41998-07-23 19:47:41 +0000142 (N.B.: the \method{read()} and \method{readline()} methods of file
Barry Warsawda00c871998-07-23 19:57:35 +0000143 objects return an empty string when they hit \EOF{}.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000144\end{excdesc}
145
Guido van Rossumdf3dba01997-10-05 18:51:26 +0000146\begin{excdesc}{FloatingPointError}
Fred Drakeb44e7531998-07-27 21:11:42 +0000147 Raised when a floating point operation fails. This exception is
148 always defined, but can only be raised when Python is configured
Fred Drakeee775a12000-04-11 19:46:40 +0000149 with the \longprogramopt{with-fpectl} option, or the
Fred Drakeb44e7531998-07-27 21:11:42 +0000150 \constant{WANT_SIGFPE_HANDLER} symbol is defined in the
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000151 \file{pyconfig.h} file.
Guido van Rossumdf3dba01997-10-05 18:51:26 +0000152\end{excdesc}
153
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000154\begin{excdesc}{IOError}
155% XXXJH xrefs here
Fred Drakeb44e7531998-07-27 21:11:42 +0000156 Raised when an I/O operation (such as a \keyword{print} statement,
157 the built-in \function{open()} function or a method of a file
158 object) fails for an I/O-related reason, e.g., ``file not found'' or
159 ``disk full''.
Guido van Rossumdf3dba01997-10-05 18:51:26 +0000160
Fred Drake02e18b41999-01-05 21:42:18 +0000161 This class is derived from \exception{EnvironmentError}. See the
Fred Drakeb44e7531998-07-27 21:11:42 +0000162 discussion above for more information on exception instance
163 attributes.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000164\end{excdesc}
165
166\begin{excdesc}{ImportError}
167% XXXJH xref to import statement?
Fred Drake27467e41998-07-23 19:47:41 +0000168 Raised when an \keyword{import} statement fails to find the module
Fred Drakef65e3231998-11-25 20:55:03 +0000169 definition or when a \code{from \textrm{\ldots} import} fails to find a
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000170 name that is to be imported.
171\end{excdesc}
172
173\begin{excdesc}{IndexError}
174% XXXJH xref to sequences
175 Raised when a sequence subscript is out of range. (Slice indices are
176 silently truncated to fall in the allowed range; if an index is not a
Fred Drake27467e41998-07-23 19:47:41 +0000177 plain integer, \exception{TypeError} is raised.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000178\end{excdesc}
179
180\begin{excdesc}{KeyError}
181% XXXJH xref to mapping objects?
182 Raised when a mapping (dictionary) key is not found in the set of
183 existing keys.
184\end{excdesc}
185
186\begin{excdesc}{KeyboardInterrupt}
187 Raised when the user hits the interrupt key (normally
Fred Drake682d5f32001-07-12 02:09:51 +0000188 \kbd{Control-C} or \kbd{Delete}). During execution, a check for
Fred Drake2a1cc3e1998-04-28 13:38:54 +0000189 interrupts is made regularly.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000190% XXXJH xrefs here
Fred Drake2a1cc3e1998-04-28 13:38:54 +0000191 Interrupts typed when a built-in function \function{input()} or
192 \function{raw_input()}) is waiting for input also raise this
Barry Warsawda00c871998-07-23 19:57:35 +0000193 exception.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000194\end{excdesc}
195
196\begin{excdesc}{MemoryError}
197 Raised when an operation runs out of memory but the situation may
198 still be rescued (by deleting some objects). The associated value is
199 a string indicating what kind of (internal) operation ran out of memory.
200 Note that because of the underlying memory management architecture
Fred Drake5828ad62000-04-06 15:03:01 +0000201 (C's \cfunction{malloc()} function), the interpreter may not
Fred Drake27467e41998-07-23 19:47:41 +0000202 always be able to completely recover from this situation; it
203 nevertheless raises an exception so that a stack traceback can be
204 printed, in case a run-away program was the cause.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000205\end{excdesc}
206
207\begin{excdesc}{NameError}
208 Raised when a local or global name is not found. This applies only
209 to unqualified names. The associated value is the name that could
210 not be found.
211\end{excdesc}
212
Barry Warsaw6d26f4b1998-12-01 19:48:04 +0000213\begin{excdesc}{NotImplementedError}
Barry Warsaw6d26f4b1998-12-01 19:48:04 +0000214 This exception is derived from \exception{RuntimeError}. In user
215 defined base classes, abstract methods should raise this exception
216 when they require derived classes to override the method.
Fred Draked0bceee1999-02-02 18:00:40 +0000217 \versionadded{1.5.2}
Barry Warsaw6d26f4b1998-12-01 19:48:04 +0000218\end{excdesc}
219
Barry Warsawda00c871998-07-23 19:57:35 +0000220\begin{excdesc}{OSError}
221 %xref for os module
Fred Drakec457ca71998-07-23 20:31:53 +0000222 This class is derived from \exception{EnvironmentError} and is used
Fred Drakeffbe6871999-04-22 21:23:22 +0000223 primarily as the \refmodule{os} module's \code{os.error} exception.
Fred Drake98be47e1999-02-01 16:17:40 +0000224 See \exception{EnvironmentError} above for a description of the
225 possible associated values.
Fred Draked0bceee1999-02-02 18:00:40 +0000226 \versionadded{1.5.2}
Barry Warsawda00c871998-07-23 19:57:35 +0000227\end{excdesc}
228
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000229\begin{excdesc}{OverflowError}
230% XXXJH reference to long's and/or int's?
231 Raised when the result of an arithmetic operation is too large to be
232 represented. This cannot occur for long integers (which would rather
Fred Drake27467e41998-07-23 19:47:41 +0000233 raise \exception{MemoryError} than give up). Because of the lack of
Fred Drake5828ad62000-04-06 15:03:01 +0000234 standardization of floating point exception handling in C, most
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000235 floating point operations also aren't checked. For plain integers,
236 all operations that can overflow are checked except left shift, where
237 typical applications prefer to drop bits than raise an exception.
238\end{excdesc}
239
240\begin{excdesc}{RuntimeError}
241 Raised when an error is detected that doesn't fall in any of the
242 other categories. The associated value is a string indicating what
Guido van Rossumdf3dba01997-10-05 18:51:26 +0000243 precisely went wrong. (This exception is mostly a relic from a
244 previous version of the interpreter; it is not used very much any
245 more.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000246\end{excdesc}
247
Fred Drake9cfe1822001-05-03 04:30:45 +0000248\begin{excdesc}{StopIteration}
249 Raised by an iterator's \method{next()} method to signal that there
250 are no further values.
251 This is derived from \exception{Exception} rather than
252 \exception{StandardError}, since this is not considered an error in
253 its normal application.
Fred Drakef42cc452001-05-03 04:39:10 +0000254 \versionadded{2.2}
Fred Drake9cfe1822001-05-03 04:30:45 +0000255\end{excdesc}
256
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000257\begin{excdesc}{SyntaxError}
258% XXXJH xref to these functions?
259 Raised when the parser encounters a syntax error. This may occur in
Fred Drake27467e41998-07-23 19:47:41 +0000260 an \keyword{import} statement, in an \keyword{exec} statement, in a call
261 to the built-in function \function{eval()} or \function{input()}, or
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000262 when reading the initial script or standard input (also
263 interactively).
Guido van Rossumdf3dba01997-10-05 18:51:26 +0000264
Fred Drakec6920552001-09-21 21:12:30 +0000265 Instances of this class have atttributes \member{filename},
266 \member{lineno}, \member{offset} and \member{text} for easier access
267 to the details. \function{str()} of the exception instance returns
268 only the message.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000269\end{excdesc}
270
271\begin{excdesc}{SystemError}
272 Raised when the interpreter finds an internal error, but the
273 situation does not look so serious to cause it to abandon all hope.
274 The associated value is a string indicating what went wrong (in
275 low-level terms).
276
277 You should report this to the author or maintainer of your Python
Fred Drakec6920552001-09-21 21:12:30 +0000278 interpreter. Be sure to report the version of the Python
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000279 interpreter (\code{sys.version}; it is also printed at the start of an
280 interactive Python session), the exact error message (the exception's
281 associated value) and if possible the source of the program that
282 triggered the error.
283\end{excdesc}
284
285\begin{excdesc}{SystemExit}
286% XXXJH xref to module sys?
Fred Drake27467e41998-07-23 19:47:41 +0000287 This exception is raised by the \function{sys.exit()} function. When it
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000288 is not handled, the Python interpreter exits; no stack traceback is
289 printed. If the associated value is a plain integer, it specifies the
Fred Drake5828ad62000-04-06 15:03:01 +0000290 system exit status (passed to C's \cfunction{exit()} function); if it is
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000291 \code{None}, the exit status is zero; if it has another type (such as
292 a string), the object's value is printed and the exit status is one.
Guido van Rossumdf3dba01997-10-05 18:51:26 +0000293
Fred Drake5828ad62000-04-06 15:03:01 +0000294 Instances have an attribute \member{code} which is set to the
295 proposed exit status or error message (defaulting to \code{None}).
296 Also, this exception derives directly from \exception{Exception} and
297 not \exception{StandardError}, since it is not technically an error.
298
Fred Drake27467e41998-07-23 19:47:41 +0000299 A call to \function{sys.exit()} is translated into an exception so that
300 clean-up handlers (\keyword{finally} clauses of \keyword{try} statements)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000301 can be executed, and so that a debugger can execute a script without
Fred Drake27467e41998-07-23 19:47:41 +0000302 running the risk of losing control. The \function{os._exit()} function
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000303 can be used if it is absolutely positively necessary to exit
Fred Drakec046e972001-07-23 19:19:39 +0000304 immediately (for example, in the child process after a call to
305 \function{fork()}).
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000306\end{excdesc}
307
308\begin{excdesc}{TypeError}
309 Raised when a built-in operation or function is applied to an object
310 of inappropriate type. The associated value is a string giving
311 details about the type mismatch.
312\end{excdesc}
313
Fred Drake5828ad62000-04-06 15:03:01 +0000314\begin{excdesc}{UnboundLocalError}
315 Raised when a reference is made to a local variable in a function or
316 method, but no value has been bound to that variable. This is a
317 subclass of \exception{NameError}.
Fred Drake30f76ff2000-06-30 16:06:19 +0000318\versionadded{2.0}
Fred Drake5828ad62000-04-06 15:03:01 +0000319\end{excdesc}
320
Fred Drake3cb793e2000-04-06 14:48:35 +0000321\begin{excdesc}{UnicodeError}
322 Raised when a Unicode-related encoding or decoding error occurs. It
323 is a subclass of \exception{ValueError}.
Fred Drake30f76ff2000-06-30 16:06:19 +0000324\versionadded{2.0}
Fred Drake3cb793e2000-04-06 14:48:35 +0000325\end{excdesc}
326
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000327\begin{excdesc}{ValueError}
328 Raised when a built-in operation or function receives an argument
329 that has the right type but an inappropriate value, and the
330 situation is not described by a more precise exception such as
Fred Drake27467e41998-07-23 19:47:41 +0000331 \exception{IndexError}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000332\end{excdesc}
333
Fred Drakecebda6f2000-04-17 17:42:00 +0000334\begin{excdesc}{WindowsError}
335 Raised when a Windows-specific error occurs or when the error number
336 does not correspond to an \cdata{errno} value. The
337 \member{errno} and \member{strerror} values are created from the
338 return values of the \cfunction{GetLastError()} and
339 \cfunction{FormatMessage()} functions from the Windows Platform API.
340 This is a subclass of \exception{OSError}.
Fred Drake30f76ff2000-06-30 16:06:19 +0000341\versionadded{2.0}
Fred Drakecebda6f2000-04-17 17:42:00 +0000342\end{excdesc}
343
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000344\begin{excdesc}{ZeroDivisionError}
345 Raised when the second argument of a division or modulo operation is
346 zero. The associated value is a string indicating the type of the
347 operands and the operation.
348\end{excdesc}
Guido van Rossum1367b832000-12-19 04:27:54 +0000349
350
Fred Drakec6920552001-09-21 21:12:30 +0000351\setindexsubitem{(built-in warning)}
Guido van Rossum1367b832000-12-19 04:27:54 +0000352
353The following exceptions are used as warning categories; see the
354\module{warnings} module for more information.
355
356\begin{excdesc}{Warning}
357Base class for warning categories.
358\end{excdesc}
359
360\begin{excdesc}{UserWarning}
361Base class for warnings generated by user code.
362\end{excdesc}
363
364\begin{excdesc}{DeprecationWarning}
365Base class for warnings about deprecated features.
366\end{excdesc}
367
368\begin{excdesc}{SyntaxWarning}
369Base class for warnings about dubious syntax
370\end{excdesc}
371
372\begin{excdesc}{RuntimeWarning}
373Base class for warnings about dubious runtime behavior.
374\end{excdesc}