Added 'description' class attribute to every command class (to help the
[python/dscho.git] / Doc / lib / libexcs.tex
blob1c57ee8d7169c93a2d67e4c1b15aba8acb35957d
1 \section{Built-in Exceptions}
3 \declaremodule{standard}{exceptions}
4 \modulesynopsis{Standard exceptions classes.}
7 Exceptions can be class objects or string objects. While
8 traditionally, most exceptions have been string objects, in Python
9 1.5, all standard exceptions have been converted to class objects,
10 and users are encouraged to do the same. The source code for those
11 exceptions is present in the standard library module
12 \module{exceptions}; this module never needs to be imported explicitly.
14 For backward compatibility, when Python is invoked with the
15 \programopt{-X} option, most of the standard exceptions are
16 strings\footnote{
17 For forward-compatibility the new exceptions \exception{Exception},
18 \exception{LookupError}, \exception{ArithmeticError},
19 \exception{EnvironmentError}, and \exception{StandardError} are
20 tuples.
21 }. This option may be used to run code that breaks because of the
22 different semantics of class based exceptions. The
23 \programopt{-X} option will become obsolete in future Python versions,
24 so the recommended solution is to fix the code.
26 Two distinct string objects with the same value are considered different
27 exceptions. This is done to force programmers to use exception names
28 rather than their string value when specifying exception handlers.
29 The string value of all built-in exceptions is their name, but this is
30 not a requirement for user-defined exceptions or exceptions defined by
31 library modules.
33 For class exceptions, in a \keyword{try} statement with an \keyword{except}
34 clause that mentions a particular class, that clause also handles
35 any exception classes derived from that class (but not exception
36 classes from which \emph{it} is derived). Two exception classes
37 that are not related via subclassing are never equivalent, even if
38 they have the same name.
39 \stindex{try}
40 \stindex{except}
42 The built-in exceptions listed below can be generated by the
43 interpreter or built-in functions. Except where mentioned, they have
44 an ``associated value'' indicating the detailed cause of the error.
45 This may be a string or a tuple containing several items of
46 information (e.g., an error code and a string explaining the code).
47 The associated value is the second argument to the \keyword{raise}
48 statement. For string exceptions, the associated value itself will be
49 stored in the variable named as the second argument of the
50 \keyword{except} clause (if any). For class exceptions, that variable
51 receives the exception instance. If the exception class is derived
52 from the standard root class \exception{Exception}, the associated
53 value is present as the exception instance's \member{args} attribute,
54 and possibly on other attributes as well.
55 \stindex{raise}
57 User code can raise built-in exceptions. This can be used to test an
58 exception handler or to report an error condition ``just like'' the
59 situation in which the interpreter raises the same exception; but
60 beware that there is nothing to prevent user code from raising an
61 inappropriate error.
63 \setindexsubitem{(built-in exception base class)}
65 The following exceptions are only used as base classes for other
66 exceptions. When string-based standard exceptions are used, they
67 are tuples containing the directly derived classes.
69 \begin{excdesc}{Exception}
70 The root class for exceptions. All built-in exceptions are derived
71 from this class. All user-defined exceptions should also be derived
72 from this class, but this is not (yet) enforced. The \function{str()}
73 function, when applied to an instance of this class (or most derived
74 classes) returns the string value of the argument or arguments, or an
75 empty string if no arguments were given to the constructor. When used
76 as a sequence, this accesses the arguments given to the constructor
77 (handy for backward compatibility with old code). The arguments are
78 also available on the instance's \member{args} attribute, as a tuple.
79 \end{excdesc}
81 \begin{excdesc}{StandardError}
82 The base class for all built-in exceptions except
83 \exception{SystemExit}. \exception{StandardError} itself is derived
84 from the root class
85 \exception{Exception}.
86 \end{excdesc}
88 \begin{excdesc}{ArithmeticError}
89 The base class for those built-in exceptions that are raised for
90 various arithmetic errors: \exception{OverflowError},
91 \exception{ZeroDivisionError}, \exception{FloatingPointError}.
92 \end{excdesc}
94 \begin{excdesc}{LookupError}
95 The base class for the exceptions that are raised when a key or
96 index used on a mapping or sequence is invalid: \exception{IndexError},
97 \exception{KeyError}.
98 \end{excdesc}
100 \begin{excdesc}{EnvironmentError}
101 The base class for exceptions that
102 can occur outside the Python system: \exception{IOError},
103 \exception{OSError}. When exceptions of this type are created with a
104 2-tuple, the first item is available on the instance's \member{errno}
105 attribute (it is assumed to be an error number), and the second item
106 is available on the \member{strerror} attribute (it is usually the
107 associated error message). The tuple itself is also available on the
108 \member{args} attribute.
109 \versionadded{1.5.2}
111 When an \exception{EnvironmentError} exception is instantiated with a
112 3-tuple, the first two items are available as above, while the third
113 item is available on the \member{filename} attribute. However, for
114 backwards compatibility, the \member{args} attribute contains only a
115 2-tuple of the first two constructor arguments.
117 The \member{filename} attribute is \code{None} when this exception is
118 created with other than 3 arguments. The \member{errno} and
119 \member{strerror} attributes are also \code{None} when the instance was
120 created with other than 2 or 3 arguments. In this last case,
121 \member{args} contains the verbatim constructor arguments as a tuple.
122 \end{excdesc}
124 \setindexsubitem{(built-in exception)}
126 The following exceptions are the exceptions that are actually raised.
127 They are class objects, except when the \programopt{-X} option is used
128 to revert back to string-based standard exceptions.
130 \begin{excdesc}{AssertionError}
131 Raised when an \keyword{assert} statement fails.
132 \stindex{assert}
133 \end{excdesc}
135 \begin{excdesc}{AttributeError}
136 % xref to attribute reference?
137 Raised when an attribute reference or assignment fails. (When an
138 object does not support attribute references or attribute assignments
139 at all, \exception{TypeError} is raised.)
140 \end{excdesc}
142 \begin{excdesc}{EOFError}
143 % XXXJH xrefs here
144 Raised when one of the built-in functions (\function{input()} or
145 \function{raw_input()}) hits an end-of-file condition (\EOF{}) without
146 reading any data.
147 % XXXJH xrefs here
148 (N.B.: the \method{read()} and \method{readline()} methods of file
149 objects return an empty string when they hit \EOF{}.)
150 \end{excdesc}
152 \begin{excdesc}{FloatingPointError}
153 Raised when a floating point operation fails. This exception is
154 always defined, but can only be raised when Python is configured
155 with the \programopt{-}\programopt{-with-fpectl} option, or the
156 \constant{WANT_SIGFPE_HANDLER} symbol is defined in the
157 \file{config.h} file.
158 \end{excdesc}
160 \begin{excdesc}{IOError}
161 % XXXJH xrefs here
162 Raised when an I/O operation (such as a \keyword{print} statement,
163 the built-in \function{open()} function or a method of a file
164 object) fails for an I/O-related reason, e.g., ``file not found'' or
165 ``disk full''.
167 This class is derived from \exception{EnvironmentError}. See the
168 discussion above for more information on exception instance
169 attributes.
170 \end{excdesc}
172 \begin{excdesc}{ImportError}
173 % XXXJH xref to import statement?
174 Raised when an \keyword{import} statement fails to find the module
175 definition or when a \code{from \textrm{\ldots} import} fails to find a
176 name that is to be imported.
177 \end{excdesc}
179 \begin{excdesc}{IndexError}
180 % XXXJH xref to sequences
181 Raised when a sequence subscript is out of range. (Slice indices are
182 silently truncated to fall in the allowed range; if an index is not a
183 plain integer, \exception{TypeError} is raised.)
184 \end{excdesc}
186 \begin{excdesc}{KeyError}
187 % XXXJH xref to mapping objects?
188 Raised when a mapping (dictionary) key is not found in the set of
189 existing keys.
190 \end{excdesc}
192 \begin{excdesc}{KeyboardInterrupt}
193 Raised when the user hits the interrupt key (normally
194 \kbd{Control-C} or \kbd{DEL}). During execution, a check for
195 interrupts is made regularly.
196 % XXXJH xrefs here
197 Interrupts typed when a built-in function \function{input()} or
198 \function{raw_input()}) is waiting for input also raise this
199 exception.
200 \end{excdesc}
202 \begin{excdesc}{MemoryError}
203 Raised when an operation runs out of memory but the situation may
204 still be rescued (by deleting some objects). The associated value is
205 a string indicating what kind of (internal) operation ran out of memory.
206 Note that because of the underlying memory management architecture
207 (\C{}'s \cfunction{malloc()} function), the interpreter may not
208 always be able to completely recover from this situation; it
209 nevertheless raises an exception so that a stack traceback can be
210 printed, in case a run-away program was the cause.
211 \end{excdesc}
213 \begin{excdesc}{NameError}
214 Raised when a local or global name is not found. This applies only
215 to unqualified names. The associated value is the name that could
216 not be found.
217 \end{excdesc}
219 \begin{excdesc}{NotImplementedError}
220 This exception is derived from \exception{RuntimeError}. In user
221 defined base classes, abstract methods should raise this exception
222 when they require derived classes to override the method.
223 \versionadded{1.5.2}
224 \end{excdesc}
226 \begin{excdesc}{OSError}
227 %xref for os module
228 This class is derived from \exception{EnvironmentError} and is used
229 primarily as the \refmodule{os} module's \code{os.error} exception.
230 See \exception{EnvironmentError} above for a description of the
231 possible associated values.
232 \versionadded{1.5.2}
233 \end{excdesc}
235 \begin{excdesc}{OverflowError}
236 % XXXJH reference to long's and/or int's?
237 Raised when the result of an arithmetic operation is too large to be
238 represented. This cannot occur for long integers (which would rather
239 raise \exception{MemoryError} than give up). Because of the lack of
240 standardization of floating point exception handling in \C{}, most
241 floating point operations also aren't checked. For plain integers,
242 all operations that can overflow are checked except left shift, where
243 typical applications prefer to drop bits than raise an exception.
244 \end{excdesc}
246 \begin{excdesc}{RuntimeError}
247 Raised when an error is detected that doesn't fall in any of the
248 other categories. The associated value is a string indicating what
249 precisely went wrong. (This exception is mostly a relic from a
250 previous version of the interpreter; it is not used very much any
251 more.)
252 \end{excdesc}
254 \begin{excdesc}{SyntaxError}
255 % XXXJH xref to these functions?
256 Raised when the parser encounters a syntax error. This may occur in
257 an \keyword{import} statement, in an \keyword{exec} statement, in a call
258 to the built-in function \function{eval()} or \function{input()}, or
259 when reading the initial script or standard input (also
260 interactively).
262 When class exceptions are used, instances of this class have
263 atttributes \member{filename}, \member{lineno}, \member{offset} and
264 \member{text} for easier access to the details; for string exceptions,
265 the associated value is usually a tuple of the form
266 \code{(message, (filename, lineno, offset, text))}.
267 For class exceptions, \function{str()} returns only the message.
268 \end{excdesc}
270 \begin{excdesc}{SystemError}
271 Raised when the interpreter finds an internal error, but the
272 situation does not look so serious to cause it to abandon all hope.
273 The associated value is a string indicating what went wrong (in
274 low-level terms).
276 You should report this to the author or maintainer of your Python
277 interpreter. Be sure to report the version string of the Python
278 interpreter (\code{sys.version}; it is also printed at the start of an
279 interactive Python session), the exact error message (the exception's
280 associated value) and if possible the source of the program that
281 triggered the error.
282 \end{excdesc}
284 \begin{excdesc}{SystemExit}
285 % XXXJH xref to module sys?
286 This exception is raised by the \function{sys.exit()} function. When it
287 is not handled, the Python interpreter exits; no stack traceback is
288 printed. If the associated value is a plain integer, it specifies the
289 system exit status (passed to \C{}'s \cfunction{exit()} function); if it is
290 \code{None}, the exit status is zero; if it has another type (such as
291 a string), the object's value is printed and the exit status is one.
293 When class exceptions are used, the instance has an attribute
294 \member{code} which is set to the proposed exit status or error message
295 (defaulting to \code{None}). Also, this exception derives directly
296 from \exception{Exception} and not \exception{StandardError}, since it
297 is not technically an error.
299 A call to \function{sys.exit()} is translated into an exception so that
300 clean-up handlers (\keyword{finally} clauses of \keyword{try} statements)
301 can be executed, and so that a debugger can execute a script without
302 running the risk of losing control. The \function{os._exit()} function
303 can be used if it is absolutely positively necessary to exit
304 immediately (e.g., after a \function{fork()} in the child process).
305 \end{excdesc}
307 \begin{excdesc}{TypeError}
308 Raised when a built-in operation or function is applied to an object
309 of inappropriate type. The associated value is a string giving
310 details about the type mismatch.
311 \end{excdesc}
313 \begin{excdesc}{ValueError}
314 Raised when a built-in operation or function receives an argument
315 that has the right type but an inappropriate value, and the
316 situation is not described by a more precise exception such as
317 \exception{IndexError}.
318 \end{excdesc}
320 \begin{excdesc}{ZeroDivisionError}
321 Raised when the second argument of a division or modulo operation is
322 zero. The associated value is a string indicating the type of the
323 operands and the operation.
324 \end{excdesc}