blob: 6dbe8184c33d6f9ab19338aa9494db982d73a061 [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
259\begin{cfuncdesc}{int}{PyErr_Warn}{PyObject *category, char *message}
260 Issue a warning message. The \var{category} argument is a warning
261 category (see below) or \NULL; the \var{message} argument is a
262 message string.
263
264 This function normally prints a warning message to \var{sys.stderr};
265 however, it is also possible that the user has specified that
266 warnings are to be turned into errors, and in that case this will
267 raise an exception. It is also possible that the function raises an
268 exception because of a problem with the warning machinery (the
269 implementation imports the \module{warnings} module to do the heavy
270 lifting). The return value is \code{0} if no exception is raised,
271 or \code{-1} if an exception is raised. (It is not possible to
272 determine whether a warning message is actually printed, nor what
273 the reason is for the exception; this is intentional.) If an
274 exception is raised, the caller should do its normal exception
275 handling (for example, \cfunction{Py_DECREF()} owned references and
276 return an error value).
277
278 Warning categories must be subclasses of \cdata{Warning}; the
279 default warning category is \cdata{RuntimeWarning}. The standard
280 Python warning categories are available as global variables whose
281 names are \samp{PyExc_} followed by the Python exception name.
282 These have the type \ctype{PyObject*}; they are all class objects.
283 Their names are \cdata{PyExc_Warning}, \cdata{PyExc_UserWarning},
Barry Warsaw29ce2d72002-08-14 16:06:28 +0000284 \cdata{PyExc_DeprecationWarning}, \cdata{PyExc_SyntaxWarning},
285 \cdata{PyExc_RuntimeWarning}, and \cdata{PyExc_FutureWarning}.
286 \cdata{PyExc_Warning} is a subclass of \cdata{PyExc_Exception}; the
287 other warning categories are subclasses of \cdata{PyExc_Warning}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000288
289 For information about warning control, see the documentation for the
290 \module{warnings} module and the \programopt{-W} option in the
291 command line documentation. There is no C API for warning control.
292\end{cfuncdesc}
293
Thomas Wouters477c8d52006-05-27 19:21:47 +0000294\begin{cfuncdesc}{int}{PyErr_WarnExplicit}{PyObject *category,
295 const char *message, const char *filename, int lineno,
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000296 const char *module, PyObject *registry}
Fred Drake3adf79e2001-10-12 19:01:43 +0000297 Issue a warning message with explicit control over all warning
298 attributes. This is a straightforward wrapper around the Python
299 function \function{warnings.warn_explicit()}, see there for more
300 information. The \var{module} and \var{registry} arguments may be
301 set to \NULL{} to get the default effect described there.
302\end{cfuncdesc}
303
304\begin{cfuncdesc}{int}{PyErr_CheckSignals}{}
305 This function interacts with Python's signal handling. It checks
306 whether a signal has been sent to the processes and if so, invokes
307 the corresponding signal handler. If the
308 \module{signal}\refbimodindex{signal} module is supported, this can
309 invoke a signal handler written in Python. In all cases, the
310 default effect for \constant{SIGINT}\ttindex{SIGINT} is to raise the
311 \withsubitem{(built-in exception)}{\ttindex{KeyboardInterrupt}}
312 \exception{KeyboardInterrupt} exception. If an exception is raised
313 the error indicator is set and the function returns \code{1};
314 otherwise the function returns \code{0}. The error indicator may or
315 may not be cleared if it was previously set.
316\end{cfuncdesc}
317
318\begin{cfuncdesc}{void}{PyErr_SetInterrupt}{}
Fred Drake043fff02004-05-12 03:20:37 +0000319 This function simulates the effect of a
Fred Drake3adf79e2001-10-12 19:01:43 +0000320 \constant{SIGINT}\ttindex{SIGINT} signal arriving --- the next time
321 \cfunction{PyErr_CheckSignals()} is called,
322 \withsubitem{(built-in exception)}{\ttindex{KeyboardInterrupt}}
323 \exception{KeyboardInterrupt} will be raised. It may be called
324 without holding the interpreter lock.
Fred Drake85309512004-03-25 14:25:28 +0000325 % XXX This was described as obsolete, but is used in
326 % thread.interrupt_main() (used from IDLE), so it's still needed.
Fred Drake3adf79e2001-10-12 19:01:43 +0000327\end{cfuncdesc}
328
329\begin{cfuncdesc}{PyObject*}{PyErr_NewException}{char *name,
330 PyObject *base,
331 PyObject *dict}
332 This utility function creates and returns a new exception object.
333 The \var{name} argument must be the name of the new exception, a C
334 string of the form \code{module.class}. The \var{base} and
335 \var{dict} arguments are normally \NULL. This creates a class
Thomas Wouters477c8d52006-05-27 19:21:47 +0000336 object derived from \exception{Exception} (accessible in C as
337 \cdata{PyExc_Exception}).
338
Fred Drake3adf79e2001-10-12 19:01:43 +0000339 The \member{__module__} attribute of the new class is set to the
340 first part (up to the last dot) of the \var{name} argument, and the
341 class name is set to the last part (after the last dot). The
Thomas Wouters477c8d52006-05-27 19:21:47 +0000342 \var{base} argument can be used to specify alternate base classes;
343 it can either be only one class or a tuple of classes.
Fred Drake3adf79e2001-10-12 19:01:43 +0000344 The \var{dict} argument can be used to specify a dictionary of class
345 variables and methods.
346\end{cfuncdesc}
347
348\begin{cfuncdesc}{void}{PyErr_WriteUnraisable}{PyObject *obj}
349 This utility function prints a warning message to \code{sys.stderr}
350 when an exception has been set but it is impossible for the
351 interpreter to actually raise the exception. It is used, for
352 example, when an exception occurs in an \method{__del__()} method.
353
354 The function is called with a single argument \var{obj} that
Raymond Hettinger2619c9e2003-12-07 11:40:17 +0000355 identifies the context in which the unraisable exception occurred.
356 The repr of \var{obj} will be printed in the warning message.
Fred Drake3adf79e2001-10-12 19:01:43 +0000357\end{cfuncdesc}
358
359\section{Standard Exceptions \label{standardExceptions}}
360
361All standard Python exceptions are available as global variables whose
362names are \samp{PyExc_} followed by the Python exception name. These
363have the type \ctype{PyObject*}; they are all class objects. For
364completeness, here are all the variables:
365
366\begin{tableiii}{l|l|c}{cdata}{C Name}{Python Name}{Notes}
Brett Cannon54ac2942006-03-01 22:10:49 +0000367 \lineiii{PyExc_BaseException\ttindex{PyExc_BaseException}}{\exception{BaseException}}{(1), (4)}
Andrew M. Kuchling6d3a0d22004-06-29 13:52:14 +0000368 \lineiii{PyExc_Exception\ttindex{PyExc_Exception}}{\exception{Exception}}{(1)}
369 \lineiii{PyExc_StandardError\ttindex{PyExc_StandardError}}{\exception{StandardError}}{(1)}
370 \lineiii{PyExc_ArithmeticError\ttindex{PyExc_ArithmeticError}}{\exception{ArithmeticError}}{(1)}
371 \lineiii{PyExc_LookupError\ttindex{PyExc_LookupError}}{\exception{LookupError}}{(1)}
372 \lineiii{PyExc_AssertionError\ttindex{PyExc_AssertionError}}{\exception{AssertionError}}{}
373 \lineiii{PyExc_AttributeError\ttindex{PyExc_AttributeError}}{\exception{AttributeError}}{}
374 \lineiii{PyExc_EOFError\ttindex{PyExc_EOFError}}{\exception{EOFError}}{}
375 \lineiii{PyExc_EnvironmentError\ttindex{PyExc_EnvironmentError}}{\exception{EnvironmentError}}{(1)}
376 \lineiii{PyExc_FloatingPointError\ttindex{PyExc_FloatingPointError}}{\exception{FloatingPointError}}{}
377 \lineiii{PyExc_IOError\ttindex{PyExc_IOError}}{\exception{IOError}}{}
378 \lineiii{PyExc_ImportError\ttindex{PyExc_ImportError}}{\exception{ImportError}}{}
379 \lineiii{PyExc_IndexError\ttindex{PyExc_IndexError}}{\exception{IndexError}}{}
380 \lineiii{PyExc_KeyError\ttindex{PyExc_KeyError}}{\exception{KeyError}}{}
381 \lineiii{PyExc_KeyboardInterrupt\ttindex{PyExc_KeyboardInterrupt}}{\exception{KeyboardInterrupt}}{}
382 \lineiii{PyExc_MemoryError\ttindex{PyExc_MemoryError}}{\exception{MemoryError}}{}
383 \lineiii{PyExc_NameError\ttindex{PyExc_NameError}}{\exception{NameError}}{}
384 \lineiii{PyExc_NotImplementedError\ttindex{PyExc_NotImplementedError}}{\exception{NotImplementedError}}{}
385 \lineiii{PyExc_OSError\ttindex{PyExc_OSError}}{\exception{OSError}}{}
386 \lineiii{PyExc_OverflowError\ttindex{PyExc_OverflowError}}{\exception{OverflowError}}{}
387 \lineiii{PyExc_ReferenceError\ttindex{PyExc_ReferenceError}}{\exception{ReferenceError}}{(2)}
388 \lineiii{PyExc_RuntimeError\ttindex{PyExc_RuntimeError}}{\exception{RuntimeError}}{}
389 \lineiii{PyExc_SyntaxError\ttindex{PyExc_SyntaxError}}{\exception{SyntaxError}}{}
390 \lineiii{PyExc_SystemError\ttindex{PyExc_SystemError}}{\exception{SystemError}}{}
391 \lineiii{PyExc_SystemExit\ttindex{PyExc_SystemExit}}{\exception{SystemExit}}{}
392 \lineiii{PyExc_TypeError\ttindex{PyExc_TypeError}}{\exception{TypeError}}{}
393 \lineiii{PyExc_ValueError\ttindex{PyExc_ValueError}}{\exception{ValueError}}{}
394 \lineiii{PyExc_WindowsError\ttindex{PyExc_WindowsError}}{\exception{WindowsError}}{(3)}
395 \lineiii{PyExc_ZeroDivisionError\ttindex{PyExc_ZeroDivisionError}}{\exception{ZeroDivisionError}}{}
Fred Drake3adf79e2001-10-12 19:01:43 +0000396\end{tableiii}
397
398\noindent
399Notes:
400\begin{description}
401\item[(1)]
402 This is a base class for other standard exceptions.
403
404\item[(2)]
405 This is the same as \exception{weakref.ReferenceError}.
406
407\item[(3)]
408 Only defined on Windows; protect code that uses this by testing that
409 the preprocessor macro \code{MS_WINDOWS} is defined.
Brett Cannon54ac2942006-03-01 22:10:49 +0000410
411\item[(4)]
412 \versionadded{2.5}
Fred Drake3adf79e2001-10-12 19:01:43 +0000413\end{description}
414
415
416\section{Deprecation of String Exceptions}
417
418All exceptions built into Python or provided in the standard library
Brett Cannon54ac2942006-03-01 22:10:49 +0000419are derived from \exception{BaseException}.
420\withsubitem{(built-in exception)}{\ttindex{BaseException}}
Fred Drake3adf79e2001-10-12 19:01:43 +0000421
422String exceptions are still supported in the interpreter to allow
Thomas Wouters477c8d52006-05-27 19:21:47 +0000423existing code to run unmodified, but this will also change in a future
Fred Drake3adf79e2001-10-12 19:01:43 +0000424release.