blob: 40cc6fd11f5145f26c80733dcf5101f9c5242ea5 [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
13provided in the built-in namespace.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000014
Guido van Rossumdf3dba01997-10-05 18:51:26 +000015Two distinct string objects with the same value are considered different
16exceptions. This is done to force programmers to use exception names
17rather than their string value when specifying exception handlers.
18The string value of all built-in exceptions is their name, but this is
19not a requirement for user-defined exceptions or exceptions defined by
20library modules.
21
Fred Drake38e5d272000-04-03 20:13:55 +000022For class exceptions, in a \keyword{try}\stindex{try} statement with
23an \keyword{except}\stindex{except} clause that mentions a particular
24class, that clause also handles any exception classes derived from
25that class (but not exception classes from which \emph{it} is
26derived). Two exception classes that are not related via subclassing
27are never equivalent, even if they have the same name.
Guido van Rossumdf3dba01997-10-05 18:51:26 +000028
29The built-in exceptions listed below can be generated by the
30interpreter or built-in functions. Except where mentioned, they have
31an ``associated value'' indicating the detailed cause of the error.
32This may be a string or a tuple containing several items of
33information (e.g., an error code and a string explaining the code).
Fred Drake38e5d272000-04-03 20:13:55 +000034The associated value is the second argument to the
35\keyword{raise}\stindex{raise} statement. For string exceptions, the
36associated value itself will be stored in the variable named as the
37second argument of the \keyword{except} clause (if any). For class
38exceptions, that variable receives the exception instance. If the
39exception class is derived from the standard root class
40\exception{Exception}, the associated value is present as the
41exception instance's \member{args} attribute, and possibly on other
42attributes as well.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000043
44User code can raise built-in exceptions. This can be used to test an
Guido van Rossumdf3dba01997-10-05 18:51:26 +000045exception handler or to report an error condition ``just like'' the
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000046situation in which the interpreter raises the same exception; but
47beware that there is nothing to prevent user code from raising an
48inappropriate error.
49
Fred Drake19479911998-02-13 06:58:54 +000050\setindexsubitem{(built-in exception base class)}
Guido van Rossumdf3dba01997-10-05 18:51:26 +000051
52The following exceptions are only used as base classes for other
Fred Drake5828ad62000-04-06 15:03:01 +000053exceptions.
Guido van Rossumdf3dba01997-10-05 18:51:26 +000054
55\begin{excdesc}{Exception}
56The root class for exceptions. All built-in exceptions are derived
57from this class. All user-defined exceptions should also be derived
Fred Drake27467e41998-07-23 19:47:41 +000058from this class, but this is not (yet) enforced. The \function{str()}
Guido van Rossumdf3dba01997-10-05 18:51:26 +000059function, when applied to an instance of this class (or most derived
60classes) returns the string value of the argument or arguments, or an
Guido van Rossum6cd7ecb1997-10-07 14:41:04 +000061empty string if no arguments were given to the constructor. When used
62as a sequence, this accesses the arguments given to the constructor
Barry Warsawda00c871998-07-23 19:57:35 +000063(handy for backward compatibility with old code). The arguments are
Fred Drakec457ca71998-07-23 20:31:53 +000064also available on the instance's \member{args} attribute, as a tuple.
Guido van Rossumdf3dba01997-10-05 18:51:26 +000065\end{excdesc}
66
67\begin{excdesc}{StandardError}
Barry Warsawf2b45541999-02-24 00:27:14 +000068The base class for all built-in exceptions except
69\exception{SystemExit}. \exception{StandardError} itself is derived
70from the root class
Fred Drake27467e41998-07-23 19:47:41 +000071\exception{Exception}.
Guido van Rossumdf3dba01997-10-05 18:51:26 +000072\end{excdesc}
73
74\begin{excdesc}{ArithmeticError}
75The base class for those built-in exceptions that are raised for
Fred Drake27467e41998-07-23 19:47:41 +000076various arithmetic errors: \exception{OverflowError},
77\exception{ZeroDivisionError}, \exception{FloatingPointError}.
Guido van Rossumdf3dba01997-10-05 18:51:26 +000078\end{excdesc}
79
80\begin{excdesc}{LookupError}
Barry Warsawda00c871998-07-23 19:57:35 +000081The base class for the exceptions that are raised when a key or
Fred Drake27467e41998-07-23 19:47:41 +000082index used on a mapping or sequence is invalid: \exception{IndexError},
83\exception{KeyError}.
Guido van Rossumdf3dba01997-10-05 18:51:26 +000084\end{excdesc}
85
Barry Warsawda00c871998-07-23 19:57:35 +000086\begin{excdesc}{EnvironmentError}
87The base class for exceptions that
88can occur outside the Python system: \exception{IOError},
89\exception{OSError}. When exceptions of this type are created with a
902-tuple, the first item is available on the instance's \member{errno}
91attribute (it is assumed to be an error number), and the second item
92is available on the \member{strerror} attribute (it is usually the
93associated error message). The tuple itself is also available on the
94\member{args} attribute.
Fred Draked0bceee1999-02-02 18:00:40 +000095\versionadded{1.5.2}
Barry Warsawda00c871998-07-23 19:57:35 +000096
97When an \exception{EnvironmentError} exception is instantiated with a
983-tuple, the first two items are available as above, while the third
99item is available on the \member{filename} attribute. However, for
100backwards compatibility, the \member{args} attribute contains only a
1012-tuple of the first two constructor arguments.
102
103The \member{filename} attribute is \code{None} when this exception is
104created with other than 3 arguments. The \member{errno} and
105\member{strerror} attributes are also \code{None} when the instance was
106created with other than 2 or 3 arguments. In this last case,
107\member{args} contains the verbatim constructor arguments as a tuple.
108\end{excdesc}
109
Fred Drake88c023b2000-09-07 16:33:32 +0000110
Fred Drake19479911998-02-13 06:58:54 +0000111\setindexsubitem{(built-in exception)}
Guido van Rossumdf3dba01997-10-05 18:51:26 +0000112
113The following exceptions are the exceptions that are actually raised.
Guido van Rossumdf3dba01997-10-05 18:51:26 +0000114
115\begin{excdesc}{AssertionError}
Guido van Rossumdf3dba01997-10-05 18:51:26 +0000116\stindex{assert}
Fred Drake38e5d272000-04-03 20:13:55 +0000117Raised when an \keyword{assert} statement fails.
Guido van Rossumdf3dba01997-10-05 18:51:26 +0000118\end{excdesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000119
120\begin{excdesc}{AttributeError}
121% xref to attribute reference?
122 Raised when an attribute reference or assignment fails. (When an
Guido van Rossum470be141995-03-17 16:07:09 +0000123 object does not support attribute references or attribute assignments
Fred Drake27467e41998-07-23 19:47:41 +0000124 at all, \exception{TypeError} is raised.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000125\end{excdesc}
126
127\begin{excdesc}{EOFError}
128% XXXJH xrefs here
Fred Drake27467e41998-07-23 19:47:41 +0000129 Raised when one of the built-in functions (\function{input()} or
130 \function{raw_input()}) hits an end-of-file condition (\EOF{}) without
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000131 reading any data.
132% XXXJH xrefs here
Fred Drake27467e41998-07-23 19:47:41 +0000133 (N.B.: the \method{read()} and \method{readline()} methods of file
Barry Warsawda00c871998-07-23 19:57:35 +0000134 objects return an empty string when they hit \EOF{}.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000135\end{excdesc}
136
Guido van Rossumdf3dba01997-10-05 18:51:26 +0000137\begin{excdesc}{FloatingPointError}
Fred Drakeb44e7531998-07-27 21:11:42 +0000138 Raised when a floating point operation fails. This exception is
139 always defined, but can only be raised when Python is configured
Fred Drakeee775a12000-04-11 19:46:40 +0000140 with the \longprogramopt{with-fpectl} option, or the
Fred Drakeb44e7531998-07-27 21:11:42 +0000141 \constant{WANT_SIGFPE_HANDLER} symbol is defined in the
142 \file{config.h} file.
Guido van Rossumdf3dba01997-10-05 18:51:26 +0000143\end{excdesc}
144
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000145\begin{excdesc}{IOError}
146% XXXJH xrefs here
Fred Drakeb44e7531998-07-27 21:11:42 +0000147 Raised when an I/O operation (such as a \keyword{print} statement,
148 the built-in \function{open()} function or a method of a file
149 object) fails for an I/O-related reason, e.g., ``file not found'' or
150 ``disk full''.
Guido van Rossumdf3dba01997-10-05 18:51:26 +0000151
Fred Drake02e18b41999-01-05 21:42:18 +0000152 This class is derived from \exception{EnvironmentError}. See the
Fred Drakeb44e7531998-07-27 21:11:42 +0000153 discussion above for more information on exception instance
154 attributes.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000155\end{excdesc}
156
157\begin{excdesc}{ImportError}
158% XXXJH xref to import statement?
Fred Drake27467e41998-07-23 19:47:41 +0000159 Raised when an \keyword{import} statement fails to find the module
Fred Drakef65e3231998-11-25 20:55:03 +0000160 definition or when a \code{from \textrm{\ldots} import} fails to find a
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000161 name that is to be imported.
162\end{excdesc}
163
164\begin{excdesc}{IndexError}
165% XXXJH xref to sequences
166 Raised when a sequence subscript is out of range. (Slice indices are
167 silently truncated to fall in the allowed range; if an index is not a
Fred Drake27467e41998-07-23 19:47:41 +0000168 plain integer, \exception{TypeError} is raised.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000169\end{excdesc}
170
171\begin{excdesc}{KeyError}
172% XXXJH xref to mapping objects?
173 Raised when a mapping (dictionary) key is not found in the set of
174 existing keys.
175\end{excdesc}
176
177\begin{excdesc}{KeyboardInterrupt}
178 Raised when the user hits the interrupt key (normally
Fred Drake2a1cc3e1998-04-28 13:38:54 +0000179 \kbd{Control-C} or \kbd{DEL}). During execution, a check for
180 interrupts is made regularly.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000181% XXXJH xrefs here
Fred Drake2a1cc3e1998-04-28 13:38:54 +0000182 Interrupts typed when a built-in function \function{input()} or
183 \function{raw_input()}) is waiting for input also raise this
Barry Warsawda00c871998-07-23 19:57:35 +0000184 exception.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000185\end{excdesc}
186
187\begin{excdesc}{MemoryError}
188 Raised when an operation runs out of memory but the situation may
189 still be rescued (by deleting some objects). The associated value is
190 a string indicating what kind of (internal) operation ran out of memory.
191 Note that because of the underlying memory management architecture
Fred Drake5828ad62000-04-06 15:03:01 +0000192 (C's \cfunction{malloc()} function), the interpreter may not
Fred Drake27467e41998-07-23 19:47:41 +0000193 always be able to completely recover from this situation; it
194 nevertheless raises an exception so that a stack traceback can be
195 printed, in case a run-away program was the cause.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000196\end{excdesc}
197
198\begin{excdesc}{NameError}
199 Raised when a local or global name is not found. This applies only
200 to unqualified names. The associated value is the name that could
201 not be found.
202\end{excdesc}
203
Barry Warsaw6d26f4b1998-12-01 19:48:04 +0000204\begin{excdesc}{NotImplementedError}
Barry Warsaw6d26f4b1998-12-01 19:48:04 +0000205 This exception is derived from \exception{RuntimeError}. In user
206 defined base classes, abstract methods should raise this exception
207 when they require derived classes to override the method.
Fred Draked0bceee1999-02-02 18:00:40 +0000208 \versionadded{1.5.2}
Barry Warsaw6d26f4b1998-12-01 19:48:04 +0000209\end{excdesc}
210
Barry Warsawda00c871998-07-23 19:57:35 +0000211\begin{excdesc}{OSError}
212 %xref for os module
Fred Drakec457ca71998-07-23 20:31:53 +0000213 This class is derived from \exception{EnvironmentError} and is used
Fred Drakeffbe6871999-04-22 21:23:22 +0000214 primarily as the \refmodule{os} module's \code{os.error} exception.
Fred Drake98be47e1999-02-01 16:17:40 +0000215 See \exception{EnvironmentError} above for a description of the
216 possible associated values.
Fred Draked0bceee1999-02-02 18:00:40 +0000217 \versionadded{1.5.2}
Barry Warsawda00c871998-07-23 19:57:35 +0000218\end{excdesc}
219
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000220\begin{excdesc}{OverflowError}
221% XXXJH reference to long's and/or int's?
222 Raised when the result of an arithmetic operation is too large to be
223 represented. This cannot occur for long integers (which would rather
Fred Drake27467e41998-07-23 19:47:41 +0000224 raise \exception{MemoryError} than give up). Because of the lack of
Fred Drake5828ad62000-04-06 15:03:01 +0000225 standardization of floating point exception handling in C, most
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000226 floating point operations also aren't checked. For plain integers,
227 all operations that can overflow are checked except left shift, where
228 typical applications prefer to drop bits than raise an exception.
229\end{excdesc}
230
231\begin{excdesc}{RuntimeError}
232 Raised when an error is detected that doesn't fall in any of the
233 other categories. The associated value is a string indicating what
Guido van Rossumdf3dba01997-10-05 18:51:26 +0000234 precisely went wrong. (This exception is mostly a relic from a
235 previous version of the interpreter; it is not used very much any
236 more.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000237\end{excdesc}
238
239\begin{excdesc}{SyntaxError}
240% XXXJH xref to these functions?
241 Raised when the parser encounters a syntax error. This may occur in
Fred Drake27467e41998-07-23 19:47:41 +0000242 an \keyword{import} statement, in an \keyword{exec} statement, in a call
243 to the built-in function \function{eval()} or \function{input()}, or
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000244 when reading the initial script or standard input (also
245 interactively).
Guido van Rossumdf3dba01997-10-05 18:51:26 +0000246
247When class exceptions are used, instances of this class have
Fred Drake27467e41998-07-23 19:47:41 +0000248atttributes \member{filename}, \member{lineno}, \member{offset} and
249\member{text} for easier access to the details; for string exceptions,
Guido van Rossumdf3dba01997-10-05 18:51:26 +0000250the associated value is usually a tuple of the form
251\code{(message, (filename, lineno, offset, text))}.
Fred Drake27467e41998-07-23 19:47:41 +0000252For class exceptions, \function{str()} returns only the message.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000253\end{excdesc}
254
255\begin{excdesc}{SystemError}
256 Raised when the interpreter finds an internal error, but the
257 situation does not look so serious to cause it to abandon all hope.
258 The associated value is a string indicating what went wrong (in
259 low-level terms).
260
261 You should report this to the author or maintainer of your Python
262 interpreter. Be sure to report the version string of the Python
263 interpreter (\code{sys.version}; it is also printed at the start of an
264 interactive Python session), the exact error message (the exception's
265 associated value) and if possible the source of the program that
266 triggered the error.
267\end{excdesc}
268
269\begin{excdesc}{SystemExit}
270% XXXJH xref to module sys?
Fred Drake27467e41998-07-23 19:47:41 +0000271 This exception is raised by the \function{sys.exit()} function. When it
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000272 is not handled, the Python interpreter exits; no stack traceback is
273 printed. If the associated value is a plain integer, it specifies the
Fred Drake5828ad62000-04-06 15:03:01 +0000274 system exit status (passed to C's \cfunction{exit()} function); if it is
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000275 \code{None}, the exit status is zero; if it has another type (such as
276 a string), the object's value is printed and the exit status is one.
Guido van Rossumdf3dba01997-10-05 18:51:26 +0000277
Fred Drake5828ad62000-04-06 15:03:01 +0000278 Instances have an attribute \member{code} which is set to the
279 proposed exit status or error message (defaulting to \code{None}).
280 Also, this exception derives directly from \exception{Exception} and
281 not \exception{StandardError}, since it is not technically an error.
282
Fred Drake27467e41998-07-23 19:47:41 +0000283 A call to \function{sys.exit()} is translated into an exception so that
284 clean-up handlers (\keyword{finally} clauses of \keyword{try} statements)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000285 can be executed, and so that a debugger can execute a script without
Fred Drake27467e41998-07-23 19:47:41 +0000286 running the risk of losing control. The \function{os._exit()} function
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000287 can be used if it is absolutely positively necessary to exit
Fred Drake27467e41998-07-23 19:47:41 +0000288 immediately (e.g., after a \function{fork()} in the child process).
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000289\end{excdesc}
290
291\begin{excdesc}{TypeError}
292 Raised when a built-in operation or function is applied to an object
293 of inappropriate type. The associated value is a string giving
294 details about the type mismatch.
295\end{excdesc}
296
Fred Drake5828ad62000-04-06 15:03:01 +0000297\begin{excdesc}{UnboundLocalError}
298 Raised when a reference is made to a local variable in a function or
299 method, but no value has been bound to that variable. This is a
300 subclass of \exception{NameError}.
Fred Drake30f76ff2000-06-30 16:06:19 +0000301\versionadded{2.0}
Fred Drake5828ad62000-04-06 15:03:01 +0000302\end{excdesc}
303
Fred Drake3cb793e2000-04-06 14:48:35 +0000304\begin{excdesc}{UnicodeError}
305 Raised when a Unicode-related encoding or decoding error occurs. It
306 is a subclass of \exception{ValueError}.
Fred Drake30f76ff2000-06-30 16:06:19 +0000307\versionadded{2.0}
Fred Drake3cb793e2000-04-06 14:48:35 +0000308\end{excdesc}
309
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000310\begin{excdesc}{ValueError}
311 Raised when a built-in operation or function receives an argument
312 that has the right type but an inappropriate value, and the
313 situation is not described by a more precise exception such as
Fred Drake27467e41998-07-23 19:47:41 +0000314 \exception{IndexError}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000315\end{excdesc}
316
Fred Drakecebda6f2000-04-17 17:42:00 +0000317\begin{excdesc}{WindowsError}
318 Raised when a Windows-specific error occurs or when the error number
319 does not correspond to an \cdata{errno} value. The
320 \member{errno} and \member{strerror} values are created from the
321 return values of the \cfunction{GetLastError()} and
322 \cfunction{FormatMessage()} functions from the Windows Platform API.
323 This is a subclass of \exception{OSError}.
Fred Drake30f76ff2000-06-30 16:06:19 +0000324\versionadded{2.0}
Fred Drakecebda6f2000-04-17 17:42:00 +0000325\end{excdesc}
326
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000327\begin{excdesc}{ZeroDivisionError}
328 Raised when the second argument of a division or modulo operation is
329 zero. The associated value is a string indicating the type of the
330 operands and the operation.
331\end{excdesc}