blob: 01c0aaf6bdac15c55cdc9f38e6a4a21302f4a6d2 [file] [log] [blame]
Fred Drake3adf79e2001-10-12 19:01:43 +00001\chapter{Exception Handling \label{exceptionHandling}}
2
3The functions described in this chapter will let you handle and raise Python
4exceptions. It is important to understand some of the basics of
5Python exception handling. It works somewhat like the
6\UNIX{} \cdata{errno} variable: there is a global indicator (per
7thread) of the last error that occurred. Most functions don't clear
8this on success, but will set it to indicate the cause of the error on
9failure. Most functions also return an error indicator, usually
10\NULL{} if they are supposed to return a pointer, or \code{-1} if they
Fred Drake551ffae2001-12-03 17:56:09 +000011return an integer (exception: the \cfunction{PyArg_*()} functions
12return \code{1} for success and \code{0} for failure).
13
14When a function must fail because some function it called failed, it
Fred Drake3adf79e2001-10-12 19:01:43 +000015generally doesn't set the error indicator; the function it called
Fred Drake551ffae2001-12-03 17:56:09 +000016already set it. It is responsible for either handling the error and
17clearing the exception or returning after cleaning up any resources it
18holds (such as object references or memory allocations); it should
19\emph{not} continue normally if it is not prepared to handle the
20error. If returning due to an error, it is important to indicate to
21the caller that an error has been set. If the error is not handled or
Thomas Helleread60e52002-12-06 22:42:13 +000022carefully propagated, additional calls into the Python/C API may not
Fred Drake551ffae2001-12-03 17:56:09 +000023behave as intended and may fail in mysterious ways.
Fred Drake3adf79e2001-10-12 19:01:43 +000024
25The error indicator consists of three Python objects corresponding to
Neal Norwitzac3625f2006-03-17 05:49:33 +000026the result of \code{sys.exc_info()}. API functions exist to interact
27with the error indicator in various ways. There is a separate
28error indicator for each thread.
Fred Drake3adf79e2001-10-12 19:01:43 +000029
30% XXX Order of these should be more thoughtful.
31% Either alphabetical or some kind of structure.
32
33\begin{cfuncdesc}{void}{PyErr_Print}{}
34 Print a standard traceback to \code{sys.stderr} and clear the error
35 indicator. Call this function only when the error indicator is
36 set. (Otherwise it will cause a fatal error!)
37\end{cfuncdesc}
38
39\begin{cfuncdesc}{PyObject*}{PyErr_Occurred}{}
40 Test whether the error indicator is set. If set, return the
41 exception \emph{type} (the first argument to the last call to one of
42 the \cfunction{PyErr_Set*()} functions or to
43 \cfunction{PyErr_Restore()}). If not set, return \NULL. You do
44 not own a reference to the return value, so you do not need to
45 \cfunction{Py_DECREF()} it. \note{Do not compare the return value
46 to a specific exception; use \cfunction{PyErr_ExceptionMatches()}
47 instead, shown below. (The comparison could easily fail since the
48 exception may be an instance instead of a class, in the case of a
49 class exception, or it may the a subclass of the expected
50 exception.)}
51\end{cfuncdesc}
52
53\begin{cfuncdesc}{int}{PyErr_ExceptionMatches}{PyObject *exc}
54 Equivalent to \samp{PyErr_GivenExceptionMatches(PyErr_Occurred(),
55 \var{exc})}. This should only be called when an exception is
56 actually set; a memory access violation will occur if no exception
57 has been raised.
58\end{cfuncdesc}
59
60\begin{cfuncdesc}{int}{PyErr_GivenExceptionMatches}{PyObject *given, PyObject *exc}
61 Return true if the \var{given} exception matches the exception in
62 \var{exc}. If \var{exc} is a class object, this also returns true
63 when \var{given} is an instance of a subclass. If \var{exc} is a
64 tuple, all exceptions in the tuple (and recursively in subtuples)
65 are searched for a match. If \var{given} is \NULL, a memory access
66 violation will occur.
67\end{cfuncdesc}
68
69\begin{cfuncdesc}{void}{PyErr_NormalizeException}{PyObject**exc, PyObject**val, PyObject**tb}
70 Under certain circumstances, the values returned by
71 \cfunction{PyErr_Fetch()} below can be ``unnormalized'', meaning
72 that \code{*\var{exc}} is a class object but \code{*\var{val}} is
73 not an instance of the same class. This function can be used to
74 instantiate the class in that case. If the values are already
75 normalized, nothing happens. The delayed normalization is
76 implemented to improve performance.
77\end{cfuncdesc}
78
79\begin{cfuncdesc}{void}{PyErr_Clear}{}
80 Clear the error indicator. If the error indicator is not set, there
81 is no effect.
82\end{cfuncdesc}
83
84\begin{cfuncdesc}{void}{PyErr_Fetch}{PyObject **ptype, PyObject **pvalue,
85 PyObject **ptraceback}
86 Retrieve the error indicator into three variables whose addresses
87 are passed. If the error indicator is not set, set all three
88 variables to \NULL. If it is set, it will be cleared and you own a
89 reference to each object retrieved. The value and traceback object
90 may be \NULL{} even when the type object is not. \note{This
91 function is normally only used by code that needs to handle
92 exceptions or by code that needs to save and restore the error
93 indicator temporarily.}
94\end{cfuncdesc}
95
96\begin{cfuncdesc}{void}{PyErr_Restore}{PyObject *type, PyObject *value,
97 PyObject *traceback}
98 Set the error indicator from the three objects. If the error
99 indicator is already set, it is cleared first. If the objects are
100 \NULL, the error indicator is cleared. Do not pass a \NULL{} type
101 and non-\NULL{} value or traceback. The exception type should be a
Neal Norwitz847207a2003-05-29 02:17:23 +0000102 class. Do not pass an invalid exception type or value.
Fred Drake3adf79e2001-10-12 19:01:43 +0000103 (Violating these rules will cause subtle problems later.) This call
104 takes away a reference to each object: you must own a reference to
105 each object before the call and after the call you no longer own
106 these references. (If you don't understand this, don't use this
107 function. I warned you.) \note{This function is normally only used
108 by code that needs to save and restore the error indicator
Fred Drake5e96f1f2002-10-24 20:54:18 +0000109 temporarily; use \cfunction{PyErr_Fetch()} to save the current
110 exception state.}
Fred Drake3adf79e2001-10-12 19:01:43 +0000111\end{cfuncdesc}
112
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000113\begin{cfuncdesc}{void}{PyErr_SetString}{PyObject *type, const char *message}
Fred Drake3adf79e2001-10-12 19:01:43 +0000114 This is the most common way to set the error indicator. The first
115 argument specifies the exception type; it is normally one of the
116 standard exceptions, e.g. \cdata{PyExc_RuntimeError}. You need not
117 increment its reference count. The second argument is an error
118 message; it is converted to a string object.
119\end{cfuncdesc}
120
121\begin{cfuncdesc}{void}{PyErr_SetObject}{PyObject *type, PyObject *value}
122 This function is similar to \cfunction{PyErr_SetString()} but lets
123 you specify an arbitrary Python object for the ``value'' of the
Fred Drake111ee322002-09-25 02:34:27 +0000124 exception.
Fred Drake3adf79e2001-10-12 19:01:43 +0000125\end{cfuncdesc}
126
127\begin{cfuncdesc}{PyObject*}{PyErr_Format}{PyObject *exception,
128 const char *format, \moreargs}
Greg Ward2748a4a2003-05-29 01:41:51 +0000129 This function sets the error indicator and returns \NULL.
Neal Norwitz847207a2003-05-29 02:17:23 +0000130 \var{exception} should be a Python exception (class, not
Fred Drakef07125e2001-12-03 16:36:43 +0000131 an instance). \var{format} should be a string, containing format
132 codes, similar to \cfunction{printf()}. The \code{width.precision}
133 before a format code is parsed, but the width part is ignored.
Fred Drake3adf79e2001-10-12 19:01:43 +0000134
Thomas Wouters477c8d52006-05-27 19:21:47 +0000135 % This should be exactly the same as the table in PyString_FromFormat.
136 % One should just refer to the other.
137
138 % The descriptions for %zd and %zu are wrong, but the truth is complicated
139 % because not all compilers support the %z width modifier -- we fake it
140 % when necessary via interpolating PY_FORMAT_SIZE_T.
141
142 % %u, %lu, %zu should have "new in Python 2.5" blurbs.
143
144 \begin{tableiii}{l|l|l}{member}{Format Characters}{Type}{Comment}
145 \lineiii{\%\%}{\emph{n/a}}{The literal \% character.}
146 \lineiii{\%c}{int}{A single character, represented as an C int.}
147 \lineiii{\%d}{int}{Exactly equivalent to \code{printf("\%d")}.}
148 \lineiii{\%u}{unsigned int}{Exactly equivalent to \code{printf("\%u")}.}
149 \lineiii{\%ld}{long}{Exactly equivalent to \code{printf("\%ld")}.}
150 \lineiii{\%lu}{unsigned long}{Exactly equivalent to \code{printf("\%lu")}.}
151 \lineiii{\%zd}{Py_ssize_t}{Exactly equivalent to \code{printf("\%zd")}.}
152 \lineiii{\%zu}{size_t}{Exactly equivalent to \code{printf("\%zu")}.}
153 \lineiii{\%i}{int}{Exactly equivalent to \code{printf("\%i")}.}
154 \lineiii{\%x}{int}{Exactly equivalent to \code{printf("\%x")}.}
155 \lineiii{\%s}{char*}{A null-terminated C character array.}
156 \lineiii{\%p}{void*}{The hex representation of a C pointer.
157 Mostly equivalent to \code{printf("\%p")} except that it is
158 guaranteed to start with the literal \code{0x} regardless of
159 what the platform's \code{printf} yields.}
160 \end{tableiii}
Fred Drake3adf79e2001-10-12 19:01:43 +0000161
162 An unrecognized format character causes all the rest of the format
163 string to be copied as-is to the result string, and any extra
164 arguments discarded.
Fred Drake3adf79e2001-10-12 19:01:43 +0000165\end{cfuncdesc}
166
167\begin{cfuncdesc}{void}{PyErr_SetNone}{PyObject *type}
168 This is a shorthand for \samp{PyErr_SetObject(\var{type},
169 Py_None)}.
170\end{cfuncdesc}
171
172\begin{cfuncdesc}{int}{PyErr_BadArgument}{}
173 This is a shorthand for \samp{PyErr_SetString(PyExc_TypeError,
174 \var{message})}, where \var{message} indicates that a built-in
175 operation was invoked with an illegal argument. It is mostly for
176 internal use.
177\end{cfuncdesc}
178
179\begin{cfuncdesc}{PyObject*}{PyErr_NoMemory}{}
180 This is a shorthand for \samp{PyErr_SetNone(PyExc_MemoryError)}; it
181 returns \NULL{} so an object allocation function can write
182 \samp{return PyErr_NoMemory();} when it runs out of memory.
183\end{cfuncdesc}
184
185\begin{cfuncdesc}{PyObject*}{PyErr_SetFromErrno}{PyObject *type}
186 This is a convenience function to raise an exception when a C
187 library function has returned an error and set the C variable
188 \cdata{errno}. It constructs a tuple object whose first item is the
189 integer \cdata{errno} value and whose second item is the
190 corresponding error message (gotten from
191 \cfunction{strerror()}\ttindex{strerror()}), and then calls
192 \samp{PyErr_SetObject(\var{type}, \var{object})}. On \UNIX, when
193 the \cdata{errno} value is \constant{EINTR}, indicating an
194 interrupted system call, this calls
195 \cfunction{PyErr_CheckSignals()}, and if that set the error
196 indicator, leaves it set to that. The function always returns
197 \NULL, so a wrapper function around a system call can write
Neil Schemenauer19415282002-03-23 20:57:11 +0000198 \samp{return PyErr_SetFromErrno(\var{type});} when the system call
199 returns an error.
Fred Drake3adf79e2001-10-12 19:01:43 +0000200\end{cfuncdesc}
201
202\begin{cfuncdesc}{PyObject*}{PyErr_SetFromErrnoWithFilename}{PyObject *type,
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000203 const char *filename}
Fred Drake3adf79e2001-10-12 19:01:43 +0000204 Similar to \cfunction{PyErr_SetFromErrno()}, with the additional
205 behavior that if \var{filename} is not \NULL, it is passed to the
206 constructor of \var{type} as a third parameter. In the case of
207 exceptions such as \exception{IOError} and \exception{OSError}, this
208 is used to define the \member{filename} attribute of the exception
209 instance.
210\end{cfuncdesc}
211
Thomas Heller4f2722a2002-07-02 15:47:03 +0000212\begin{cfuncdesc}{PyObject*}{PyErr_SetFromWindowsErr}{int ierr}
Fred Drakeabe7c1a2002-07-02 16:17:58 +0000213 This is a convenience function to raise \exception{WindowsError}.
214 If called with \var{ierr} of \cdata{0}, the error code returned by a
215 call to \cfunction{GetLastError()} is used instead. It calls the
216 Win32 function \cfunction{FormatMessage()} to retrieve the Windows
217 description of error code given by \var{ierr} or
218 \cfunction{GetLastError()}, then it constructs a tuple object whose
219 first item is the \var{ierr} value and whose second item is the
220 corresponding error message (gotten from
Thomas Heller4f2722a2002-07-02 15:47:03 +0000221 \cfunction{FormatMessage()}), and then calls
222 \samp{PyErr_SetObject(\var{PyExc_WindowsError}, \var{object})}.
Fred Drakeabe7c1a2002-07-02 16:17:58 +0000223 This function always returns \NULL.
224 Availability: Windows.
Thomas Heller4f2722a2002-07-02 15:47:03 +0000225\end{cfuncdesc}
226
Thomas Heller085358a2002-07-29 14:27:41 +0000227\begin{cfuncdesc}{PyObject*}{PyErr_SetExcFromWindowsErr}{PyObject *type,
228 int ierr}
229 Similar to \cfunction{PyErr_SetFromWindowsErr()}, with an additional
230 parameter specifying the exception type to be raised.
231 Availability: Windows.
232 \versionadded{2.3}
233\end{cfuncdesc}
234
Thomas Heller4f2722a2002-07-02 15:47:03 +0000235\begin{cfuncdesc}{PyObject*}{PyErr_SetFromWindowsErrWithFilename}{int ierr,
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000236 const char *filename}
Thomas Heller4f2722a2002-07-02 15:47:03 +0000237 Similar to \cfunction{PyErr_SetFromWindowsErr()}, with the
238 additional behavior that if \var{filename} is not \NULL, it is
239 passed to the constructor of \exception{WindowsError} as a third
Fred Drakeabe7c1a2002-07-02 16:17:58 +0000240 parameter.
241 Availability: Windows.
Thomas Heller4f2722a2002-07-02 15:47:03 +0000242\end{cfuncdesc}
243
Thomas Heller085358a2002-07-29 14:27:41 +0000244\begin{cfuncdesc}{PyObject*}{PyErr_SetExcFromWindowsErrWithFilename}
245 {PyObject *type, int ierr, char *filename}
246 Similar to \cfunction{PyErr_SetFromWindowsErrWithFilename()}, with
247 an additional parameter specifying the exception type to be raised.
248 Availability: Windows.
249 \versionadded{2.3}
250\end{cfuncdesc}
251
Fred Drake3adf79e2001-10-12 19:01:43 +0000252\begin{cfuncdesc}{void}{PyErr_BadInternalCall}{}
253 This is a shorthand for \samp{PyErr_SetString(PyExc_TypeError,
254 \var{message})}, where \var{message} indicates that an internal
255 operation (e.g. a Python/C API function) was invoked with an illegal
256 argument. It is mostly for internal use.
257\end{cfuncdesc}
258
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000259\begin{cfuncdesc}{int}{PyErr_WarnEx}{PyObject *category, char *message, int stacklevel}
Fred Drake3adf79e2001-10-12 19:01:43 +0000260 Issue a warning message. The \var{category} argument is a warning
261 category (see below) or \NULL; the \var{message} argument is a
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000262 message string. \var{stacklevel} is a positive number giving a
263 number of stack frames; the warning will be issued from the
264 currently executing line of code in that stack frame. A \var{stacklevel}
265 of 1 is the function calling \cfunction{PyErr_WarnEx()}, 2 is
266 the function above that, and so forth.
Fred Drake3adf79e2001-10-12 19:01:43 +0000267
268 This function normally prints a warning message to \var{sys.stderr};
269 however, it is also possible that the user has specified that
270 warnings are to be turned into errors, and in that case this will
271 raise an exception. It is also possible that the function raises an
272 exception because of a problem with the warning machinery (the
273 implementation imports the \module{warnings} module to do the heavy
274 lifting). The return value is \code{0} if no exception is raised,
275 or \code{-1} if an exception is raised. (It is not possible to
276 determine whether a warning message is actually printed, nor what
277 the reason is for the exception; this is intentional.) If an
278 exception is raised, the caller should do its normal exception
279 handling (for example, \cfunction{Py_DECREF()} owned references and
280 return an error value).
281
282 Warning categories must be subclasses of \cdata{Warning}; the
283 default warning category is \cdata{RuntimeWarning}. The standard
284 Python warning categories are available as global variables whose
285 names are \samp{PyExc_} followed by the Python exception name.
286 These have the type \ctype{PyObject*}; they are all class objects.
287 Their names are \cdata{PyExc_Warning}, \cdata{PyExc_UserWarning},
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000288 \cdata{PyExc_UnicodeWarning}, \cdata{PyExc_DeprecationWarning},
289 \cdata{PyExc_SyntaxWarning}, \cdata{PyExc_RuntimeWarning}, and
290 \cdata{PyExc_FutureWarning}. \cdata{PyExc_Warning} is a subclass of
291 \cdata{PyExc_Exception}; the other warning categories are subclasses
292 of \cdata{PyExc_Warning}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000293
294 For information about warning control, see the documentation for the
295 \module{warnings} module and the \programopt{-W} option in the
296 command line documentation. There is no C API for warning control.
297\end{cfuncdesc}
298
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000299\begin{cfuncdesc}{int}{PyErr_Warn}{PyObject *category, char *message}
300 Issue a warning message. The \var{category} argument is a warning
301 category (see below) or \NULL; the \var{message} argument is a
302 message string. The warning will appear to be issued from the function
303 calling \cfunction{PyErr_Warn()}, equivalent to calling
304 \cfunction{PyErr_WarnEx()} with a \var{stacklevel} of 1.
305
306 Deprecated; use \cfunction{PyErr_WarnEx()} instead.
307\end{cfuncdesc}
308
Thomas Wouters477c8d52006-05-27 19:21:47 +0000309\begin{cfuncdesc}{int}{PyErr_WarnExplicit}{PyObject *category,
310 const char *message, const char *filename, int lineno,
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000311 const char *module, PyObject *registry}
Fred Drake3adf79e2001-10-12 19:01:43 +0000312 Issue a warning message with explicit control over all warning
313 attributes. This is a straightforward wrapper around the Python
314 function \function{warnings.warn_explicit()}, see there for more
315 information. The \var{module} and \var{registry} arguments may be
316 set to \NULL{} to get the default effect described there.
317\end{cfuncdesc}
318
319\begin{cfuncdesc}{int}{PyErr_CheckSignals}{}
320 This function interacts with Python's signal handling. It checks
321 whether a signal has been sent to the processes and if so, invokes
322 the corresponding signal handler. If the
323 \module{signal}\refbimodindex{signal} module is supported, this can
324 invoke a signal handler written in Python. In all cases, the
325 default effect for \constant{SIGINT}\ttindex{SIGINT} is to raise the
326 \withsubitem{(built-in exception)}{\ttindex{KeyboardInterrupt}}
327 \exception{KeyboardInterrupt} exception. If an exception is raised
Thomas Wouters89f507f2006-12-13 04:49:30 +0000328 the error indicator is set and the function returns \code{-1};
Fred Drake3adf79e2001-10-12 19:01:43 +0000329 otherwise the function returns \code{0}. The error indicator may or
330 may not be cleared if it was previously set.
331\end{cfuncdesc}
332
333\begin{cfuncdesc}{void}{PyErr_SetInterrupt}{}
Fred Drake043fff02004-05-12 03:20:37 +0000334 This function simulates the effect of a
Fred Drake3adf79e2001-10-12 19:01:43 +0000335 \constant{SIGINT}\ttindex{SIGINT} signal arriving --- the next time
336 \cfunction{PyErr_CheckSignals()} is called,
337 \withsubitem{(built-in exception)}{\ttindex{KeyboardInterrupt}}
338 \exception{KeyboardInterrupt} will be raised. It may be called
339 without holding the interpreter lock.
Fred Drake85309512004-03-25 14:25:28 +0000340 % XXX This was described as obsolete, but is used in
341 % thread.interrupt_main() (used from IDLE), so it's still needed.
Fred Drake3adf79e2001-10-12 19:01:43 +0000342\end{cfuncdesc}
343
344\begin{cfuncdesc}{PyObject*}{PyErr_NewException}{char *name,
345 PyObject *base,
346 PyObject *dict}
347 This utility function creates and returns a new exception object.
348 The \var{name} argument must be the name of the new exception, a C
349 string of the form \code{module.class}. The \var{base} and
350 \var{dict} arguments are normally \NULL. This creates a class
Thomas Wouters477c8d52006-05-27 19:21:47 +0000351 object derived from \exception{Exception} (accessible in C as
352 \cdata{PyExc_Exception}).
353
Fred Drake3adf79e2001-10-12 19:01:43 +0000354 The \member{__module__} attribute of the new class is set to the
355 first part (up to the last dot) of the \var{name} argument, and the
356 class name is set to the last part (after the last dot). The
Thomas Wouters477c8d52006-05-27 19:21:47 +0000357 \var{base} argument can be used to specify alternate base classes;
358 it can either be only one class or a tuple of classes.
Fred Drake3adf79e2001-10-12 19:01:43 +0000359 The \var{dict} argument can be used to specify a dictionary of class
360 variables and methods.
361\end{cfuncdesc}
362
363\begin{cfuncdesc}{void}{PyErr_WriteUnraisable}{PyObject *obj}
364 This utility function prints a warning message to \code{sys.stderr}
365 when an exception has been set but it is impossible for the
366 interpreter to actually raise the exception. It is used, for
367 example, when an exception occurs in an \method{__del__()} method.
368
369 The function is called with a single argument \var{obj} that
Raymond Hettinger2619c9e2003-12-07 11:40:17 +0000370 identifies the context in which the unraisable exception occurred.
371 The repr of \var{obj} will be printed in the warning message.
Fred Drake3adf79e2001-10-12 19:01:43 +0000372\end{cfuncdesc}
373
374\section{Standard Exceptions \label{standardExceptions}}
375
376All standard Python exceptions are available as global variables whose
377names are \samp{PyExc_} followed by the Python exception name. These
378have the type \ctype{PyObject*}; they are all class objects. For
379completeness, here are all the variables:
380
381\begin{tableiii}{l|l|c}{cdata}{C Name}{Python Name}{Notes}
Brett Cannon54ac2942006-03-01 22:10:49 +0000382 \lineiii{PyExc_BaseException\ttindex{PyExc_BaseException}}{\exception{BaseException}}{(1), (4)}
Andrew M. Kuchling6d3a0d22004-06-29 13:52:14 +0000383 \lineiii{PyExc_Exception\ttindex{PyExc_Exception}}{\exception{Exception}}{(1)}
Andrew M. Kuchling6d3a0d22004-06-29 13:52:14 +0000384 \lineiii{PyExc_ArithmeticError\ttindex{PyExc_ArithmeticError}}{\exception{ArithmeticError}}{(1)}
385 \lineiii{PyExc_LookupError\ttindex{PyExc_LookupError}}{\exception{LookupError}}{(1)}
386 \lineiii{PyExc_AssertionError\ttindex{PyExc_AssertionError}}{\exception{AssertionError}}{}
387 \lineiii{PyExc_AttributeError\ttindex{PyExc_AttributeError}}{\exception{AttributeError}}{}
388 \lineiii{PyExc_EOFError\ttindex{PyExc_EOFError}}{\exception{EOFError}}{}
389 \lineiii{PyExc_EnvironmentError\ttindex{PyExc_EnvironmentError}}{\exception{EnvironmentError}}{(1)}
390 \lineiii{PyExc_FloatingPointError\ttindex{PyExc_FloatingPointError}}{\exception{FloatingPointError}}{}
391 \lineiii{PyExc_IOError\ttindex{PyExc_IOError}}{\exception{IOError}}{}
392 \lineiii{PyExc_ImportError\ttindex{PyExc_ImportError}}{\exception{ImportError}}{}
393 \lineiii{PyExc_IndexError\ttindex{PyExc_IndexError}}{\exception{IndexError}}{}
394 \lineiii{PyExc_KeyError\ttindex{PyExc_KeyError}}{\exception{KeyError}}{}
395 \lineiii{PyExc_KeyboardInterrupt\ttindex{PyExc_KeyboardInterrupt}}{\exception{KeyboardInterrupt}}{}
396 \lineiii{PyExc_MemoryError\ttindex{PyExc_MemoryError}}{\exception{MemoryError}}{}
397 \lineiii{PyExc_NameError\ttindex{PyExc_NameError}}{\exception{NameError}}{}
398 \lineiii{PyExc_NotImplementedError\ttindex{PyExc_NotImplementedError}}{\exception{NotImplementedError}}{}
399 \lineiii{PyExc_OSError\ttindex{PyExc_OSError}}{\exception{OSError}}{}
400 \lineiii{PyExc_OverflowError\ttindex{PyExc_OverflowError}}{\exception{OverflowError}}{}
401 \lineiii{PyExc_ReferenceError\ttindex{PyExc_ReferenceError}}{\exception{ReferenceError}}{(2)}
402 \lineiii{PyExc_RuntimeError\ttindex{PyExc_RuntimeError}}{\exception{RuntimeError}}{}
403 \lineiii{PyExc_SyntaxError\ttindex{PyExc_SyntaxError}}{\exception{SyntaxError}}{}
404 \lineiii{PyExc_SystemError\ttindex{PyExc_SystemError}}{\exception{SystemError}}{}
405 \lineiii{PyExc_SystemExit\ttindex{PyExc_SystemExit}}{\exception{SystemExit}}{}
406 \lineiii{PyExc_TypeError\ttindex{PyExc_TypeError}}{\exception{TypeError}}{}
407 \lineiii{PyExc_ValueError\ttindex{PyExc_ValueError}}{\exception{ValueError}}{}
408 \lineiii{PyExc_WindowsError\ttindex{PyExc_WindowsError}}{\exception{WindowsError}}{(3)}
409 \lineiii{PyExc_ZeroDivisionError\ttindex{PyExc_ZeroDivisionError}}{\exception{ZeroDivisionError}}{}
Fred Drake3adf79e2001-10-12 19:01:43 +0000410\end{tableiii}
411
412\noindent
413Notes:
414\begin{description}
415\item[(1)]
416 This is a base class for other standard exceptions.
417
418\item[(2)]
419 This is the same as \exception{weakref.ReferenceError}.
420
421\item[(3)]
422 Only defined on Windows; protect code that uses this by testing that
423 the preprocessor macro \code{MS_WINDOWS} is defined.
Brett Cannon54ac2942006-03-01 22:10:49 +0000424
425\item[(4)]
426 \versionadded{2.5}
Fred Drake3adf79e2001-10-12 19:01:43 +0000427\end{description}
428
429
430\section{Deprecation of String Exceptions}
431
432All exceptions built into Python or provided in the standard library
Brett Cannon54ac2942006-03-01 22:10:49 +0000433are derived from \exception{BaseException}.
434\withsubitem{(built-in exception)}{\ttindex{BaseException}}
Fred Drake3adf79e2001-10-12 19:01:43 +0000435
436String exceptions are still supported in the interpreter to allow
Thomas Wouters477c8d52006-05-27 19:21:47 +0000437existing code to run unmodified, but this will also change in a future
Fred Drake3adf79e2001-10-12 19:01:43 +0000438release.