blob: 8c15a95a49ce2eb73e7ca5616490722b105e4b0f [file] [log] [blame]
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001\section{Built-in Exceptions}
Fred Drake3bd9ab01998-07-23 19:33:08 +00002\declaremodule{standard}{exceptions}
3
4\modulesynopsis{Standard exceptions classes.}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00005
Fred Drakeb91e9341998-07-23 17:59:49 +00006
Guido van Rossumdf3dba01997-10-05 18:51:26 +00007Exceptions can be class objects or string objects. While
8traditionally, most exceptions have been string objects, in Python
Guido van Rossumeb0f0661997-12-30 20:38:16 +000091.5, all standard exceptions have been converted to class objects,
Barry Warsawda00c871998-07-23 19:57:35 +000010and users are encouraged to do the same. The source code for those
Guido van Rossumdf3dba01997-10-05 18:51:26 +000011exceptions is present in the standard library module
Fred Drake27467e41998-07-23 19:47:41 +000012\module{exceptions}; this module never needs to be imported explicitly.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000013
Guido van Rossumdf3dba01997-10-05 18:51:26 +000014For backward compatibility, when Python is invoked with the \code{-X}
Barry Warsawda00c871998-07-23 19:57:35 +000015option, most of the standard exceptions are strings\footnote{For
Fred Drakec457ca71998-07-23 20:31:53 +000016forward-compatibility the new exceptions \exception{LookupError},
17\exception{ArithmeticError}, \exception{EnvironmentError}, and
18\exception{StandardError} are tuples.}. This option may be used to
Barry Warsawda00c871998-07-23 19:57:35 +000019run code that breaks because of the different semantics of class based
20exceptions. The \code{-X} option will become obsolete in future
Guido van Rossumdf3dba01997-10-05 18:51:26 +000021Python versions, so the recommended solution is to fix the code.
22
23Two distinct string objects with the same value are considered different
24exceptions. This is done to force programmers to use exception names
25rather than their string value when specifying exception handlers.
26The string value of all built-in exceptions is their name, but this is
27not a requirement for user-defined exceptions or exceptions defined by
28library modules.
29
Fred Drake27467e41998-07-23 19:47:41 +000030For class exceptions, in a \keyword{try} statement with an \keyword{except}
Guido van Rossumdf3dba01997-10-05 18:51:26 +000031clause that mentions a particular class, that clause also handles
32any exception classes derived from that class (but not exception
33classes from which \emph{it} is derived). Two exception classes
34that are not related via subclassing are never equivalent, even if
35they have the same name.
36\stindex{try}
37\stindex{except}
38
39The built-in exceptions listed below can be generated by the
40interpreter or built-in functions. Except where mentioned, they have
41an ``associated value'' indicating the detailed cause of the error.
42This may be a string or a tuple containing several items of
43information (e.g., an error code and a string explaining the code).
Fred Drake27467e41998-07-23 19:47:41 +000044The associated value is the second argument to the \keyword{raise}
Guido van Rossumdf3dba01997-10-05 18:51:26 +000045statement. For string exceptions, the associated value itself will be
46stored in the variable named as the second argument of the
Barry Warsawda00c871998-07-23 19:57:35 +000047\keyword{except} clause (if any). For class exceptions, that variable
48receives the exception instance. If the exception class is derived
49from the standard root class \exception{Exception}, the associated
50value is present as the exception instance's \member{args} attribute,
51and possibly on other attributes as well.
Guido van Rossumdf3dba01997-10-05 18:51:26 +000052\stindex{raise}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000053
54User code can raise built-in exceptions. This can be used to test an
Guido van Rossumdf3dba01997-10-05 18:51:26 +000055exception handler or to report an error condition ``just like'' the
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000056situation in which the interpreter raises the same exception; but
57beware that there is nothing to prevent user code from raising an
58inappropriate error.
59
Fred Drake19479911998-02-13 06:58:54 +000060\setindexsubitem{(built-in exception base class)}
Guido van Rossumdf3dba01997-10-05 18:51:26 +000061
62The following exceptions are only used as base classes for other
63exceptions. When string-based standard exceptions are used, they
64are tuples containing the directly derived classes.
65
66\begin{excdesc}{Exception}
67The root class for exceptions. All built-in exceptions are derived
68from this class. All user-defined exceptions should also be derived
Fred Drake27467e41998-07-23 19:47:41 +000069from this class, but this is not (yet) enforced. The \function{str()}
Guido van Rossumdf3dba01997-10-05 18:51:26 +000070function, when applied to an instance of this class (or most derived
71classes) returns the string value of the argument or arguments, or an
Guido van Rossum6cd7ecb1997-10-07 14:41:04 +000072empty string if no arguments were given to the constructor. When used
73as a sequence, this accesses the arguments given to the constructor
Barry Warsawda00c871998-07-23 19:57:35 +000074(handy for backward compatibility with old code). The arguments are
Fred Drakec457ca71998-07-23 20:31:53 +000075also available on the instance's \member{args} attribute, as a tuple.
Guido van Rossumdf3dba01997-10-05 18:51:26 +000076\end{excdesc}
77
78\begin{excdesc}{StandardError}
79The base class for built-in exceptions. All built-in exceptions are
80derived from this class, which is itself derived from the root class
Fred Drake27467e41998-07-23 19:47:41 +000081\exception{Exception}.
Guido van Rossumdf3dba01997-10-05 18:51:26 +000082\end{excdesc}
83
84\begin{excdesc}{ArithmeticError}
85The base class for those built-in exceptions that are raised for
Fred Drake27467e41998-07-23 19:47:41 +000086various arithmetic errors: \exception{OverflowError},
87\exception{ZeroDivisionError}, \exception{FloatingPointError}.
Guido van Rossumdf3dba01997-10-05 18:51:26 +000088\end{excdesc}
89
90\begin{excdesc}{LookupError}
Barry Warsawda00c871998-07-23 19:57:35 +000091The base class for the exceptions that are raised when a key or
Fred Drake27467e41998-07-23 19:47:41 +000092index used on a mapping or sequence is invalid: \exception{IndexError},
93\exception{KeyError}.
Guido van Rossumdf3dba01997-10-05 18:51:26 +000094\end{excdesc}
95
Barry Warsawda00c871998-07-23 19:57:35 +000096\begin{excdesc}{EnvironmentError}
97The base class for exceptions that
98can occur outside the Python system: \exception{IOError},
99\exception{OSError}. When exceptions of this type are created with a
1002-tuple, the first item is available on the instance's \member{errno}
101attribute (it is assumed to be an error number), and the second item
102is available on the \member{strerror} attribute (it is usually the
103associated error message). The tuple itself is also available on the
104\member{args} attribute.
Fred Draked0bceee1999-02-02 18:00:40 +0000105\versionadded{1.5.2}
Barry Warsawda00c871998-07-23 19:57:35 +0000106
107When an \exception{EnvironmentError} exception is instantiated with a
1083-tuple, the first two items are available as above, while the third
109item is available on the \member{filename} attribute. However, for
110backwards compatibility, the \member{args} attribute contains only a
1112-tuple of the first two constructor arguments.
112
113The \member{filename} attribute is \code{None} when this exception is
114created with other than 3 arguments. The \member{errno} and
115\member{strerror} attributes are also \code{None} when the instance was
116created with other than 2 or 3 arguments. In this last case,
117\member{args} contains the verbatim constructor arguments as a tuple.
118\end{excdesc}
119
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.
123They are class objects, except when the \code{-X} option is used to
124revert back to string-based standard exceptions.
125
126\begin{excdesc}{AssertionError}
Fred Drake27467e41998-07-23 19:47:41 +0000127Raised when an \keyword{assert} statement fails.
Guido van Rossumdf3dba01997-10-05 18:51:26 +0000128\stindex{assert}
129\end{excdesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000130
131\begin{excdesc}{AttributeError}
132% xref to attribute reference?
133 Raised when an attribute reference or assignment fails. (When an
Guido van Rossum470be141995-03-17 16:07:09 +0000134 object does not support attribute references or attribute assignments
Fred Drake27467e41998-07-23 19:47:41 +0000135 at all, \exception{TypeError} is raised.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000136\end{excdesc}
137
138\begin{excdesc}{EOFError}
139% XXXJH xrefs here
Fred Drake27467e41998-07-23 19:47:41 +0000140 Raised when one of the built-in functions (\function{input()} or
141 \function{raw_input()}) hits an end-of-file condition (\EOF{}) without
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000142 reading any data.
143% XXXJH xrefs here
Fred Drake27467e41998-07-23 19:47:41 +0000144 (N.B.: the \method{read()} and \method{readline()} methods of file
Barry Warsawda00c871998-07-23 19:57:35 +0000145 objects return an empty string when they hit \EOF{}.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000146\end{excdesc}
147
Guido van Rossumdf3dba01997-10-05 18:51:26 +0000148\begin{excdesc}{FloatingPointError}
Fred Drakeb44e7531998-07-27 21:11:42 +0000149 Raised when a floating point operation fails. This exception is
150 always defined, but can only be raised when Python is configured
151 with the \code{--with-fpectl} option, or the
152 \constant{WANT_SIGFPE_HANDLER} symbol is defined in the
153 \file{config.h} file.
Guido van Rossumdf3dba01997-10-05 18:51:26 +0000154\end{excdesc}
155
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000156\begin{excdesc}{IOError}
157% XXXJH xrefs here
Fred Drakeb44e7531998-07-27 21:11:42 +0000158 Raised when an I/O operation (such as a \keyword{print} statement,
159 the built-in \function{open()} function or a method of a file
160 object) fails for an I/O-related reason, e.g., ``file not found'' or
161 ``disk full''.
Guido van Rossumdf3dba01997-10-05 18:51:26 +0000162
Fred Drake02e18b41999-01-05 21:42:18 +0000163 This class is derived from \exception{EnvironmentError}. See the
Fred Drakeb44e7531998-07-27 21:11:42 +0000164 discussion above for more information on exception instance
165 attributes.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000166\end{excdesc}
167
168\begin{excdesc}{ImportError}
169% XXXJH xref to import statement?
Fred Drake27467e41998-07-23 19:47:41 +0000170 Raised when an \keyword{import} statement fails to find the module
Fred Drakef65e3231998-11-25 20:55:03 +0000171 definition or when a \code{from \textrm{\ldots} import} fails to find a
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000172 name that is to be imported.
173\end{excdesc}
174
175\begin{excdesc}{IndexError}
176% XXXJH xref to sequences
177 Raised when a sequence subscript is out of range. (Slice indices are
178 silently truncated to fall in the allowed range; if an index is not a
Fred Drake27467e41998-07-23 19:47:41 +0000179 plain integer, \exception{TypeError} is raised.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000180\end{excdesc}
181
182\begin{excdesc}{KeyError}
183% XXXJH xref to mapping objects?
184 Raised when a mapping (dictionary) key is not found in the set of
185 existing keys.
186\end{excdesc}
187
188\begin{excdesc}{KeyboardInterrupt}
189 Raised when the user hits the interrupt key (normally
Fred Drake2a1cc3e1998-04-28 13:38:54 +0000190 \kbd{Control-C} or \kbd{DEL}). During execution, a check for
191 interrupts is made regularly.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000192% XXXJH xrefs here
Fred Drake2a1cc3e1998-04-28 13:38:54 +0000193 Interrupts typed when a built-in function \function{input()} or
194 \function{raw_input()}) is waiting for input also raise this
Barry Warsawda00c871998-07-23 19:57:35 +0000195 exception.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000196\end{excdesc}
197
198\begin{excdesc}{MemoryError}
199 Raised when an operation runs out of memory but the situation may
200 still be rescued (by deleting some objects). The associated value is
201 a string indicating what kind of (internal) operation ran out of memory.
202 Note that because of the underlying memory management architecture
Fred Drake27467e41998-07-23 19:47:41 +0000203 (\C{}'s \cfunction{malloc()} function), the interpreter may not
204 always be able to completely recover from this situation; it
205 nevertheless raises an exception so that a stack traceback can be
206 printed, in case a run-away program was the cause.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000207\end{excdesc}
208
209\begin{excdesc}{NameError}
210 Raised when a local or global name is not found. This applies only
211 to unqualified names. The associated value is the name that could
212 not be found.
213\end{excdesc}
214
Barry Warsaw6d26f4b1998-12-01 19:48:04 +0000215\begin{excdesc}{NotImplementedError}
Barry Warsaw6d26f4b1998-12-01 19:48:04 +0000216 This exception is derived from \exception{RuntimeError}. In user
217 defined base classes, abstract methods should raise this exception
218 when they require derived classes to override the method.
Fred Draked0bceee1999-02-02 18:00:40 +0000219 \versionadded{1.5.2}
Barry Warsaw6d26f4b1998-12-01 19:48:04 +0000220\end{excdesc}
221
Barry Warsawda00c871998-07-23 19:57:35 +0000222\begin{excdesc}{OSError}
223 %xref for os module
Fred Drakec457ca71998-07-23 20:31:53 +0000224 This class is derived from \exception{EnvironmentError} and is used
Fred Drake98be47e1999-02-01 16:17:40 +0000225 primarily as the \module{os} module's \code{os.error} exception.
226 See \exception{EnvironmentError} above for a description of the
227 possible associated values.
Fred Draked0bceee1999-02-02 18:00:40 +0000228 \versionadded{1.5.2}
Barry Warsawda00c871998-07-23 19:57:35 +0000229\end{excdesc}
230
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000231\begin{excdesc}{OverflowError}
232% XXXJH reference to long's and/or int's?
233 Raised when the result of an arithmetic operation is too large to be
234 represented. This cannot occur for long integers (which would rather
Fred Drake27467e41998-07-23 19:47:41 +0000235 raise \exception{MemoryError} than give up). Because of the lack of
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000236 standardization of floating point exception handling in \C{}, most
237 floating point operations also aren't checked. For plain integers,
238 all operations that can overflow are checked except left shift, where
239 typical applications prefer to drop bits than raise an exception.
240\end{excdesc}
241
242\begin{excdesc}{RuntimeError}
243 Raised when an error is detected that doesn't fall in any of the
244 other categories. The associated value is a string indicating what
Guido van Rossumdf3dba01997-10-05 18:51:26 +0000245 precisely went wrong. (This exception is mostly a relic from a
246 previous version of the interpreter; it is not used very much any
247 more.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000248\end{excdesc}
249
250\begin{excdesc}{SyntaxError}
251% XXXJH xref to these functions?
252 Raised when the parser encounters a syntax error. This may occur in
Fred Drake27467e41998-07-23 19:47:41 +0000253 an \keyword{import} statement, in an \keyword{exec} statement, in a call
254 to the built-in function \function{eval()} or \function{input()}, or
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000255 when reading the initial script or standard input (also
256 interactively).
Guido van Rossumdf3dba01997-10-05 18:51:26 +0000257
258When class exceptions are used, instances of this class have
Fred Drake27467e41998-07-23 19:47:41 +0000259atttributes \member{filename}, \member{lineno}, \member{offset} and
260\member{text} for easier access to the details; for string exceptions,
Guido van Rossumdf3dba01997-10-05 18:51:26 +0000261the associated value is usually a tuple of the form
262\code{(message, (filename, lineno, offset, text))}.
Fred Drake27467e41998-07-23 19:47:41 +0000263For class exceptions, \function{str()} returns only the message.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000264\end{excdesc}
265
266\begin{excdesc}{SystemError}
267 Raised when the interpreter finds an internal error, but the
268 situation does not look so serious to cause it to abandon all hope.
269 The associated value is a string indicating what went wrong (in
270 low-level terms).
271
272 You should report this to the author or maintainer of your Python
273 interpreter. Be sure to report the version string of the Python
274 interpreter (\code{sys.version}; it is also printed at the start of an
275 interactive Python session), the exact error message (the exception's
276 associated value) and if possible the source of the program that
277 triggered the error.
278\end{excdesc}
279
280\begin{excdesc}{SystemExit}
281% XXXJH xref to module sys?
Fred Drake27467e41998-07-23 19:47:41 +0000282 This exception is raised by the \function{sys.exit()} function. When it
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000283 is not handled, the Python interpreter exits; no stack traceback is
284 printed. If the associated value is a plain integer, it specifies the
Fred Drake27467e41998-07-23 19:47:41 +0000285 system exit status (passed to \C{}'s \cfunction{exit()} function); if it is
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000286 \code{None}, the exit status is zero; if it has another type (such as
287 a string), the object's value is printed and the exit status is one.
Guido van Rossumdf3dba01997-10-05 18:51:26 +0000288
289When class exceptions are used, the instance has an attribute
Fred Drake27467e41998-07-23 19:47:41 +0000290\member{code} which is set to the proposed exit status or error message
Guido van Rossumdf3dba01997-10-05 18:51:26 +0000291(defaulting to \code{None}).
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000292
Fred Drake27467e41998-07-23 19:47:41 +0000293 A call to \function{sys.exit()} is translated into an exception so that
294 clean-up handlers (\keyword{finally} clauses of \keyword{try} statements)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000295 can be executed, and so that a debugger can execute a script without
Fred Drake27467e41998-07-23 19:47:41 +0000296 running the risk of losing control. The \function{os._exit()} function
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000297 can be used if it is absolutely positively necessary to exit
Fred Drake27467e41998-07-23 19:47:41 +0000298 immediately (e.g., after a \function{fork()} in the child process).
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000299\end{excdesc}
300
301\begin{excdesc}{TypeError}
302 Raised when a built-in operation or function is applied to an object
303 of inappropriate type. The associated value is a string giving
304 details about the type mismatch.
305\end{excdesc}
306
307\begin{excdesc}{ValueError}
308 Raised when a built-in operation or function receives an argument
309 that has the right type but an inappropriate value, and the
310 situation is not described by a more precise exception such as
Fred Drake27467e41998-07-23 19:47:41 +0000311 \exception{IndexError}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000312\end{excdesc}
313
314\begin{excdesc}{ZeroDivisionError}
315 Raised when the second argument of a division or modulo operation is
316 zero. The associated value is a string indicating the type of the
317 operands and the operation.
318\end{excdesc}