1 \section{Built-in Exceptions
}
3 Exceptions are string objects. Two distinct string objects with the
4 same value are different exceptions. This is done to force programmers
5 to use exception names rather than their string value when specifying
6 exception handlers. The string value of all built-in exceptions is
7 their name, but this is not a requirement for user-defined exceptions
8 or exceptions defined by library modules.
10 The following exceptions can be generated by the interpreter or
11 built-in functions. Except where mentioned, they have an `associated
12 value' indicating the detailed cause of the error. This may be a
13 string or a tuple containing several items of information (e.g., an
14 error code and a string explaining the code).
16 User code can raise built-in exceptions. This can be used to test an
17 exception handler or to
report an error condition `just like' the
18 situation in which the interpreter raises the same exception; but
19 beware that there is nothing to prevent user code from raising an
22 \renewcommand{\indexsubitem}{(built-in exception)
}
24 \begin{excdesc
}{AttributeError
}
25 % xref to attribute reference?
26 Raised when an attribute reference or assignment fails. (When an
27 object does not support attributes references or attribute assignments
28 at all,
\code{TypeError
} is raised.)
31 \begin{excdesc
}{EOFError
}
33 Raised when one of the built-in functions (
\code{input()
} or
34 \code{raw_input()
}) hits an end-of-file condition (
\EOF{}) without
37 (N.B.: the
\code{read()
} and
\code{readline()
} methods of file
38 objects return an empty string when they hit
\EOF{}.) No associated value.
41 \begin{excdesc
}{IOError
}
43 Raised when an I/O operation (such as a
\code{print
} statement, the
44 built-in
\code{open()
} function or a method of a file object) fails
45 for an I/O-related reason, e.g., `file not found', `disk full'.
48 \begin{excdesc
}{ImportError
}
49 % XXXJH xref to import statement?
50 Raised when an
\code{import
} statement fails to find the module
51 definition or when a
\code{from
{\rm \ldots} import
} fails to find a
52 name that is to be imported.
55 \begin{excdesc
}{IndexError
}
56 % XXXJH xref to sequences
57 Raised when a sequence subscript is out of range. (Slice indices are
58 silently truncated to fall in the allowed range; if an index is not a
59 plain integer,
\code{TypeError
} is raised.)
62 \begin{excdesc
}{KeyError
}
63 % XXXJH xref to mapping objects?
64 Raised when a mapping (dictionary) key is not found in the set of
68 \begin{excdesc
}{KeyboardInterrupt
}
69 Raised when the user hits the interrupt key (normally
71 \key{DEL
}). During execution, a check for interrupts is made regularly.
73 Interrupts typed when a built-in function
\code{input()
} or
74 \code{raw_input()
}) is waiting for input also raise this exception. No
78 \begin{excdesc
}{MemoryError
}
79 Raised when an operation runs out of memory but the situation may
80 still be rescued (by deleting some objects). The associated value is
81 a string indicating what kind of (internal) operation ran out of memory.
82 Note that because of the underlying memory management architecture
83 (
\C{}'s
\code{malloc()
} function), the interpreter may not always be able
84 to completely recover from this situation; it nevertheless raises an
85 exception so that a stack traceback can be printed, in case a run-away
86 program was the cause.
89 \begin{excdesc
}{NameError
}
90 Raised when a local or global name is not found. This applies only
91 to unqualified names. The associated value is the name that could
95 \begin{excdesc
}{OverflowError
}
96 % XXXJH reference to long's and/or int's?
97 Raised when the result of an arithmetic operation is too large to be
98 represented. This cannot occur for long integers (which would rather
99 raise
\code{MemoryError
} than give up). Because of the lack of
100 standardization of floating point exception handling in
\C{}, most
101 floating point operations also aren't checked. For plain integers,
102 all operations that can overflow are checked except left shift, where
103 typical applications prefer to drop bits than raise an exception.
106 \begin{excdesc
}{RuntimeError
}
107 Raised when an error is detected that doesn't fall in any of the
108 other categories. The associated value is a string indicating what
109 precisely went wrong. (This exception is a relic from a previous
110 version of the interpreter; it is not used any more except by some
111 extension modules that haven't been converted to define their own
115 \begin{excdesc
}{SyntaxError
}
116 % XXXJH xref to these functions?
117 Raised when the parser encounters a syntax error. This may occur in
118 an
\code{import
} statement, in an
\code{exec
} statement, in a call
119 to the built-in function
\code{eval()
} or
\code{input()
}, or
120 when reading the initial script or standard input (also
124 \begin{excdesc
}{SystemError
}
125 Raised when the interpreter finds an internal error, but the
126 situation does not look so serious to cause it to abandon all hope.
127 The associated value is a string indicating what went wrong (in
130 You should
report this to the author or maintainer of your Python
131 interpreter. Be sure to
report the version string of the Python
132 interpreter (
\code{sys.version
}; it is also printed at the start of an
133 interactive Python session), the exact error message (the exception's
134 associated value) and if possible the source of the program that
138 \begin{excdesc
}{SystemExit
}
139 % XXXJH xref to module sys?
140 This exception is raised by the
\code{sys.exit()
} function. When it
141 is not handled, the Python interpreter exits; no stack traceback is
142 printed. If the associated value is a plain integer, it specifies the
143 system exit status (passed to
\C{}'s
\code{exit()
} function); if it is
144 \code{None
}, the exit status is zero; if it has another type (such as
145 a string), the object's value is printed and the exit status is one.
147 A call to
\code{sys.exit
} is translated into an exception so that
148 clean-up handlers (
\code{finally
} clauses of
\code{try
} statements)
149 can be executed, and so that a debugger can execute a script without
150 running the risk of losing control. The
\code{posix._exit()
} function
151 can be used if it is absolutely positively necessary to exit
152 immediately (e.g., after a
\code{fork()
} in the child process).
155 \begin{excdesc
}{TypeError
}
156 Raised when a built-in operation or function is applied to an object
157 of inappropriate type. The associated value is a string giving
158 details about the type mismatch.
161 \begin{excdesc
}{ValueError
}
162 Raised when a built-in operation or function receives an argument
163 that has the right type but an inappropriate value, and the
164 situation is not described by a more precise exception such as
168 \begin{excdesc
}{ZeroDivisionError
}
169 Raised when the second argument of a division or modulo operation is
170 zero. The associated value is a string indicating the type of the
171 operands and the operation.