blob: 1a6ba4072277d2820348206fb11d8a343ab141c5 [file] [log] [blame]
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001\section{Built-in Exceptions}
2
Guido van Rossumdf3dba01997-10-05 18:51:26 +00003Exceptions can be class objects or string objects. While
4traditionally, most exceptions have been string objects, in Python
51.5a4, all standard exceptions have been converted to class objects,
6and users are encouraged to the the same. The source code for those
7exceptions is present in the standard library module
8\code{exceptions}; this module never needs to be imported explicitly.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00009
Guido van Rossumdf3dba01997-10-05 18:51:26 +000010For backward compatibility, when Python is invoked with the \code{-X}
11option, the standard exceptions are strings. This may be needed to
12run some code that breaks because of the different semantics of class
13based exceptions. The \code{-X} option will become obsolete in future
14Python versions, so the recommended solution is to fix the code.
15
16Two 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
23For class exceptions, in a \code{try} statement with an\code{except}
24clause that mentions a particular class, that clause also handles
25any exception classes derived from that class (but not exception
26classes from which \emph{it} is derived). Two exception classes
27that are not related via subclassing are never equivalent, even if
28they have the same name.
29\stindex{try}
30\stindex{except}
31
32The built-in exceptions listed below can be generated by the
33interpreter or built-in functions. Except where mentioned, they have
34an ``associated value'' indicating the detailed cause of the error.
35This may be a string or a tuple containing several items of
36information (e.g., an error code and a string explaining the code).
37The associated value is the second argument to the \code{raise}
38statement. For string exceptions, the associated value itself will be
39stored in the variable named as the second argument of the
40\code{except} clause (if any). For class exceptions derived from
41the root class \code{Exception}, that variable receives the exception
42instance, and the associated value is present as the exception
43instance's \code{args} attribute; this is a tuple even if the second
44argument to \code{raise} was not (then it is a singleton tuple).
45\stindex{raise}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000046
47User code can raise built-in exceptions. This can be used to test an
Guido van Rossumdf3dba01997-10-05 18:51:26 +000048exception handler or to report an error condition ``just like'' the
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000049situation in which the interpreter raises the same exception; but
50beware that there is nothing to prevent user code from raising an
51inappropriate error.
52
Guido van Rossumdf3dba01997-10-05 18:51:26 +000053\renewcommand{\indexsubitem}{(built-in exception base class)}
54
55The following exceptions are only used as base classes for other
56exceptions. When string-based standard exceptions are used, they
57are tuples containing the directly derived classes.
58
59\begin{excdesc}{Exception}
60The root class for exceptions. All built-in exceptions are derived
61from this class. All user-defined exceptions should also be derived
62from this class, but this is not (yet) enforced. The \code{str()}
63function, when applied to an instance of this class (or most derived
64classes) returns the string value of the argument or arguments, or an
Guido van Rossum6cd7ecb1997-10-07 14:41:04 +000065empty string if no arguments were given to the constructor. When used
66as a sequence, this accesses the arguments given to the constructor
67(handy for backward compatibility with old code).
Guido van Rossumdf3dba01997-10-05 18:51:26 +000068\end{excdesc}
69
70\begin{excdesc}{StandardError}
71The base class for built-in exceptions. All built-in exceptions are
72derived from this class, which is itself derived from the root class
Guido van Rossum6cd7ecb1997-10-07 14:41:04 +000073\code{Exception}.
Guido van Rossumdf3dba01997-10-05 18:51:26 +000074\end{excdesc}
75
76\begin{excdesc}{ArithmeticError}
77The base class for those built-in exceptions that are raised for
78various arithmetic errors: \code{OverflowError},
79\code{ZeroDivisionError}, \code{FloatingPointError}.
80\end{excdesc}
81
82\begin{excdesc}{LookupError}
83The base class for thise exceptions that are raised when a key or
84index used on a mapping or sequence is invalid: \code{IndexError},
85\code{KeyError}.
86\end{excdesc}
87
Fred Drake8e079981997-12-16 14:54:32 +000088\renewcommand{\indexsubitem}{(built-in exception)}
Guido van Rossumdf3dba01997-10-05 18:51:26 +000089
90The following exceptions are the exceptions that are actually raised.
91They are class objects, except when the \code{-X} option is used to
92revert back to string-based standard exceptions.
93
94\begin{excdesc}{AssertionError}
95Raised when an \code{assert} statement fails.
96\stindex{assert}
97\end{excdesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000098
99\begin{excdesc}{AttributeError}
100% xref to attribute reference?
101 Raised when an attribute reference or assignment fails. (When an
Guido van Rossum470be141995-03-17 16:07:09 +0000102 object does not support attribute references or attribute assignments
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000103 at all, \code{TypeError} is raised.)
104\end{excdesc}
105
106\begin{excdesc}{EOFError}
107% XXXJH xrefs here
108 Raised when one of the built-in functions (\code{input()} or
109 \code{raw_input()}) hits an end-of-file condition (\EOF{}) without
110 reading any data.
111% XXXJH xrefs here
112 (N.B.: the \code{read()} and \code{readline()} methods of file
113 objects return an empty string when they hit \EOF{}.) No associated value.
114\end{excdesc}
115
Guido van Rossumdf3dba01997-10-05 18:51:26 +0000116\begin{excdesc}{FloatingPointError}
117Raised when a floating point operation fails. This exception is
118always defined, but can only be raised when Python is configured with
119the \code{--with-fpectl} option, or the \code{WANT_SIGFPE_HANDLER}
120symbol is defined in the \file{config.h} file.
121\end{excdesc}
122
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000123\begin{excdesc}{IOError}
124% XXXJH xrefs here
125 Raised when an I/O operation (such as a \code{print} statement, the
126 built-in \code{open()} function or a method of a file object) fails
Guido van Rossumdf3dba01997-10-05 18:51:26 +0000127 for an I/O-related reason, e.g., ``file not found'' or ``disk full''.
128
129When class exceptions are used, and this exception is instantiated as
130\code{IOError(errno, strerror)}, the instance has two additional
131attributes \code{errno} and \code{strerror} set to the error code and
132the error message, respectively. These attributes default to
133\code{None}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000134\end{excdesc}
135
136\begin{excdesc}{ImportError}
137% XXXJH xref to import statement?
138 Raised when an \code{import} statement fails to find the module
139 definition or when a \code{from {\rm \ldots} import} fails to find a
140 name that is to be imported.
141\end{excdesc}
142
143\begin{excdesc}{IndexError}
144% XXXJH xref to sequences
145 Raised when a sequence subscript is out of range. (Slice indices are
146 silently truncated to fall in the allowed range; if an index is not a
147 plain integer, \code{TypeError} is raised.)
148\end{excdesc}
149
150\begin{excdesc}{KeyError}
151% XXXJH xref to mapping objects?
152 Raised when a mapping (dictionary) key is not found in the set of
153 existing keys.
154\end{excdesc}
155
156\begin{excdesc}{KeyboardInterrupt}
157 Raised when the user hits the interrupt key (normally
158 \kbd{Control-C} or
159\key{DEL}). During execution, a check for interrupts is made regularly.
160% XXXJH xrefs here
161 Interrupts typed when a built-in function \code{input()} or
162 \code{raw_input()}) is waiting for input also raise this exception. No
163 associated value.
164\end{excdesc}
165
166\begin{excdesc}{MemoryError}
167 Raised when an operation runs out of memory but the situation may
168 still be rescued (by deleting some objects). The associated value is
169 a string indicating what kind of (internal) operation ran out of memory.
170 Note that because of the underlying memory management architecture
171 (\C{}'s \code{malloc()} function), the interpreter may not always be able
172 to completely recover from this situation; it nevertheless raises an
173 exception so that a stack traceback can be printed, in case a run-away
174 program was the cause.
175\end{excdesc}
176
177\begin{excdesc}{NameError}
178 Raised when a local or global name is not found. This applies only
179 to unqualified names. The associated value is the name that could
180 not be found.
181\end{excdesc}
182
183\begin{excdesc}{OverflowError}
184% XXXJH reference to long's and/or int's?
185 Raised when the result of an arithmetic operation is too large to be
186 represented. This cannot occur for long integers (which would rather
187 raise \code{MemoryError} than give up). Because of the lack of
188 standardization of floating point exception handling in \C{}, most
189 floating point operations also aren't checked. For plain integers,
190 all operations that can overflow are checked except left shift, where
191 typical applications prefer to drop bits than raise an exception.
192\end{excdesc}
193
194\begin{excdesc}{RuntimeError}
195 Raised when an error is detected that doesn't fall in any of the
196 other categories. The associated value is a string indicating what
Guido van Rossumdf3dba01997-10-05 18:51:26 +0000197 precisely went wrong. (This exception is mostly a relic from a
198 previous version of the interpreter; it is not used very much any
199 more.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000200\end{excdesc}
201
202\begin{excdesc}{SyntaxError}
203% XXXJH xref to these functions?
204 Raised when the parser encounters a syntax error. This may occur in
205 an \code{import} statement, in an \code{exec} statement, in a call
206 to the built-in function \code{eval()} or \code{input()}, or
207 when reading the initial script or standard input (also
208 interactively).
Guido van Rossumdf3dba01997-10-05 18:51:26 +0000209
210When class exceptions are used, instances of this class have
211atttributes \code{filename}, \code{lineno}, \code{offset} and
212\code{text} for easier access to the details; for string exceptions,
213the associated value is usually a tuple of the form
214\code{(message, (filename, lineno, offset, text))}.
215For class exceptions, \code{str()} returns only the message.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000216\end{excdesc}
217
218\begin{excdesc}{SystemError}
219 Raised when the interpreter finds an internal error, but the
220 situation does not look so serious to cause it to abandon all hope.
221 The associated value is a string indicating what went wrong (in
222 low-level terms).
223
224 You should report this to the author or maintainer of your Python
225 interpreter. Be sure to report the version string of the Python
226 interpreter (\code{sys.version}; it is also printed at the start of an
227 interactive Python session), the exact error message (the exception's
228 associated value) and if possible the source of the program that
229 triggered the error.
230\end{excdesc}
231
232\begin{excdesc}{SystemExit}
233% XXXJH xref to module sys?
234 This exception is raised by the \code{sys.exit()} function. When it
235 is not handled, the Python interpreter exits; no stack traceback is
236 printed. If the associated value is a plain integer, it specifies the
237 system exit status (passed to \C{}'s \code{exit()} function); if it is
238 \code{None}, the exit status is zero; if it has another type (such as
239 a string), the object's value is printed and the exit status is one.
Guido van Rossumdf3dba01997-10-05 18:51:26 +0000240
241When class exceptions are used, the instance has an attribute
242\code{code} which is set to the proposed exit status or error message
243(defaulting to \code{None}).
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000244
Fred Drake8e079981997-12-16 14:54:32 +0000245 A call to \code{sys.exit()} is translated into an exception so that
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000246 clean-up handlers (\code{finally} clauses of \code{try} statements)
247 can be executed, and so that a debugger can execute a script without
Guido van Rossumdf3dba01997-10-05 18:51:26 +0000248 running the risk of losing control. The \code{os._exit()} function
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000249 can be used if it is absolutely positively necessary to exit
250 immediately (e.g., after a \code{fork()} in the child process).
251\end{excdesc}
252
253\begin{excdesc}{TypeError}
254 Raised when a built-in operation or function is applied to an object
255 of inappropriate type. The associated value is a string giving
256 details about the type mismatch.
257\end{excdesc}
258
259\begin{excdesc}{ValueError}
260 Raised when a built-in operation or function receives an argument
261 that has the right type but an inappropriate value, and the
262 situation is not described by a more precise exception such as
263 \code{IndexError}.
264\end{excdesc}
265
266\begin{excdesc}{ZeroDivisionError}
267 Raised when the second argument of a division or modulo operation is
268 zero. The associated value is a string indicating the type of the
269 operands and the operation.
270\end{excdesc}