<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Exception Handling — Python v2.6.5 documentation</title> <link rel="stylesheet" href="../_static/default.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../', VERSION: '2.6.5', COLLAPSE_MODINDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <link rel="search" type="application/opensearchdescription+xml" title="Search within Python v2.6.5 documentation" href="../_static/opensearch.xml"/> <link rel="author" title="About these documents" href="../about.html" /> <link rel="copyright" title="Copyright" href="../copyright.html" /> <link rel="top" title="Python v2.6.5 documentation" href="../index.html" /> <link rel="up" title="Python/C API Reference Manual" href="index.html" /> <link rel="next" title="Utilities" href="utilities.html" /> <link rel="prev" title="Reference Counting" href="refcounting.html" /> <link rel="shortcut icon" type="image/png" href="../_static/py.png" /> </head> <body> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="../modindex.html" title="Global Module Index" accesskey="M">modules</a> |</li> <li class="right" > <a href="utilities.html" title="Utilities" accesskey="N">next</a> |</li> <li class="right" > <a href="refcounting.html" title="Reference Counting" accesskey="P">previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="../index.html">Python v2.6.5 documentation</a> »</li> <li><a href="index.html" accesskey="U">Python/C API Reference Manual</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="exception-handling"> <span id="exceptionhandling"></span><h1>Exception Handling<a class="headerlink" href="#exception-handling" title="Permalink to this headline">¶</a></h1> <p>The functions described in this chapter will let you handle and raise Python exceptions. It is important to understand some of the basics of Python exception handling. It works somewhat like the Unix <tt class="xref docutils literal"><span class="pre">errno</span></tt> variable: there is a global indicator (per thread) of the last error that occurred. Most functions don’t clear this on success, but will set it to indicate the cause of the error on failure. Most functions also return an error indicator, usually <em>NULL</em> if they are supposed to return a pointer, or <tt class="docutils literal"><span class="pre">-1</span></tt> if they return an integer (exception: the <tt class="xref docutils literal"><span class="pre">PyArg_*()</span></tt> functions return <tt class="docutils literal"><span class="pre">1</span></tt> for success and <tt class="docutils literal"><span class="pre">0</span></tt> for failure).</p> <p>When a function must fail because some function it called failed, it generally doesn’t set the error indicator; the function it called already set it. It is responsible for either handling the error and clearing the exception or returning after cleaning up any resources it holds (such as object references or memory allocations); it should <em>not</em> continue normally if it is not prepared to handle the error. If returning due to an error, it is important to indicate to the caller that an error has been set. If the error is not handled or carefully propagated, additional calls into the Python/C API may not behave as intended and may fail in mysterious ways.</p> <p id="index-16">The error indicator consists of three Python objects corresponding to the Python variables <tt class="docutils literal"><span class="pre">sys.exc_type</span></tt>, <tt class="docutils literal"><span class="pre">sys.exc_value</span></tt> and <tt class="docutils literal"><span class="pre">sys.exc_traceback</span></tt>. API functions exist to interact with the error indicator in various ways. There is a separate error indicator for each thread.</p> <dl class="cfunction"> <dt id="PyErr_PrintEx"> void <tt class="descname">PyErr_PrintEx</tt><big>(</big>int<em> set_sys_last_vars</em><big>)</big><a class="headerlink" href="#PyErr_PrintEx" title="Permalink to this definition">¶</a></dt> <dd><p>Print a standard traceback to <tt class="docutils literal"><span class="pre">sys.stderr</span></tt> and clear the error indicator. Call this function only when the error indicator is set. (Otherwise it will cause a fatal error!)</p> <p>If <em>set_sys_last_vars</em> is nonzero, the variables <a title="sys.last_type" class="reference external" href="../library/sys.html#sys.last_type"><tt class="xref docutils literal"><span class="pre">sys.last_type</span></tt></a>, <a title="sys.last_value" class="reference external" href="../library/sys.html#sys.last_value"><tt class="xref docutils literal"><span class="pre">sys.last_value</span></tt></a> and <a title="sys.last_traceback" class="reference external" href="../library/sys.html#sys.last_traceback"><tt class="xref docutils literal"><span class="pre">sys.last_traceback</span></tt></a> will be set to the type, value and traceback of the printed exception, respectively.</p> </dd></dl> <dl class="cfunction"> <dt id="PyErr_Print"> void <tt class="descname">PyErr_Print</tt><big>(</big><big>)</big><a class="headerlink" href="#PyErr_Print" title="Permalink to this definition">¶</a></dt> <dd>Alias for <tt class="docutils literal"><span class="pre">PyErr_PrintEx(1)</span></tt>.</dd></dl> <dl class="cfunction"> <dt id="PyErr_Occurred"> <a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyErr_Occurred</tt><big>(</big><big>)</big><a class="headerlink" href="#PyErr_Occurred" title="Permalink to this definition">¶</a></dt> <dd><em class="refcount">Return value: Borrowed reference.</em><p>Test whether the error indicator is set. If set, return the exception <em>type</em> (the first argument to the last call to one of the <tt class="xref docutils literal"><span class="pre">PyErr_Set*()</span></tt> functions or to <a title="PyErr_Restore" class="reference internal" href="#PyErr_Restore"><tt class="xref docutils literal"><span class="pre">PyErr_Restore()</span></tt></a>). If not set, return <em>NULL</em>. You do not own a reference to the return value, so you do not need to <a title="Py_DECREF" class="reference external" href="refcounting.html#Py_DECREF"><tt class="xref docutils literal"><span class="pre">Py_DECREF()</span></tt></a> it.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">Do not compare the return value to a specific exception; use <a title="PyErr_ExceptionMatches" class="reference internal" href="#PyErr_ExceptionMatches"><tt class="xref docutils literal"><span class="pre">PyErr_ExceptionMatches()</span></tt></a> instead, shown below. (The comparison could easily fail since the exception may be an instance instead of a class, in the case of a class exception, or it may the a subclass of the expected exception.)</p> </div> </dd></dl> <dl class="cfunction"> <dt id="PyErr_ExceptionMatches"> int <tt class="descname">PyErr_ExceptionMatches</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *exc</em><big>)</big><a class="headerlink" href="#PyErr_ExceptionMatches" title="Permalink to this definition">¶</a></dt> <dd>Equivalent to <tt class="docutils literal"><span class="pre">PyErr_GivenExceptionMatches(PyErr_Occurred(),</span> <span class="pre">exc)</span></tt>. This should only be called when an exception is actually set; a memory access violation will occur if no exception has been raised.</dd></dl> <dl class="cfunction"> <dt id="PyErr_GivenExceptionMatches"> int <tt class="descname">PyErr_GivenExceptionMatches</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *given</em>, <a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *exc</em><big>)</big><a class="headerlink" href="#PyErr_GivenExceptionMatches" title="Permalink to this definition">¶</a></dt> <dd>Return true if the <em>given</em> exception matches the exception in <em>exc</em>. If <em>exc</em> is a class object, this also returns true when <em>given</em> is an instance of a subclass. If <em>exc</em> is a tuple, all exceptions in the tuple (and recursively in subtuples) are searched for a match.</dd></dl> <dl class="cfunction"> <dt id="PyErr_NormalizeException"> void <tt class="descname">PyErr_NormalizeException</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>**exc, <a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>**val, <a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>**tb<big>)</big><a class="headerlink" href="#PyErr_NormalizeException" title="Permalink to this definition">¶</a></dt> <dd>Under certain circumstances, the values returned by <a title="PyErr_Fetch" class="reference internal" href="#PyErr_Fetch"><tt class="xref docutils literal"><span class="pre">PyErr_Fetch()</span></tt></a> below can be “unnormalized”, meaning that <tt class="docutils literal"><span class="pre">*exc</span></tt> is a class object but <tt class="docutils literal"><span class="pre">*val</span></tt> is not an instance of the same class. This function can be used to instantiate the class in that case. If the values are already normalized, nothing happens. The delayed normalization is implemented to improve performance.</dd></dl> <dl class="cfunction"> <dt id="PyErr_Clear"> void <tt class="descname">PyErr_Clear</tt><big>(</big><big>)</big><a class="headerlink" href="#PyErr_Clear" title="Permalink to this definition">¶</a></dt> <dd>Clear the error indicator. If the error indicator is not set, there is no effect.</dd></dl> <dl class="cfunction"> <dt id="PyErr_Fetch"> void <tt class="descname">PyErr_Fetch</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> **ptype</em>, <a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> **pvalue</em>, <a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> **ptraceback</em><big>)</big><a class="headerlink" href="#PyErr_Fetch" title="Permalink to this definition">¶</a></dt> <dd><p>Retrieve the error indicator into three variables whose addresses are passed. If the error indicator is not set, set all three variables to <em>NULL</em>. If it is set, it will be cleared and you own a reference to each object retrieved. The value and traceback object may be <em>NULL</em> even when the type object is not.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">This function is normally only used by code that needs to handle exceptions or by code that needs to save and restore the error indicator temporarily.</p> </div> </dd></dl> <dl class="cfunction"> <dt id="PyErr_Restore"> void <tt class="descname">PyErr_Restore</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *type</em>, <a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *value</em>, <a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *traceback</em><big>)</big><a class="headerlink" href="#PyErr_Restore" title="Permalink to this definition">¶</a></dt> <dd><p>Set the error indicator from the three objects. If the error indicator is already set, it is cleared first. If the objects are <em>NULL</em>, the error indicator is cleared. Do not pass a <em>NULL</em> type and non-<em>NULL</em> value or traceback. The exception type should be a class. Do not pass an invalid exception type or value. (Violating these rules will cause subtle problems later.) This call takes away a reference to each object: you must own a reference to each object before the call and after the call you no longer own these references. (If you don’t understand this, don’t use this function. I warned you.)</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">This function is normally only used by code that needs to save and restore the error indicator temporarily; use <a title="PyErr_Fetch" class="reference internal" href="#PyErr_Fetch"><tt class="xref docutils literal"><span class="pre">PyErr_Fetch()</span></tt></a> to save the current exception state.</p> </div> </dd></dl> <dl class="cfunction"> <dt id="PyErr_SetString"> void <tt class="descname">PyErr_SetString</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *type</em>, const char<em> *message</em><big>)</big><a class="headerlink" href="#PyErr_SetString" title="Permalink to this definition">¶</a></dt> <dd>This is the most common way to set the error indicator. The first argument specifies the exception type; it is normally one of the standard exceptions, e.g. <tt class="xref docutils literal"><span class="pre">PyExc_RuntimeError</span></tt>. You need not increment its reference count. The second argument is an error message; it is converted to a string object.</dd></dl> <dl class="cfunction"> <dt id="PyErr_SetObject"> void <tt class="descname">PyErr_SetObject</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *type</em>, <a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *value</em><big>)</big><a class="headerlink" href="#PyErr_SetObject" title="Permalink to this definition">¶</a></dt> <dd>This function is similar to <a title="PyErr_SetString" class="reference internal" href="#PyErr_SetString"><tt class="xref docutils literal"><span class="pre">PyErr_SetString()</span></tt></a> but lets you specify an arbitrary Python object for the “value” of the exception.</dd></dl> <dl class="cfunction"> <dt id="PyErr_Format"> <a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyErr_Format</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *exception</em>, const char<em> *format</em>, ...<big>)</big><a class="headerlink" href="#PyErr_Format" title="Permalink to this definition">¶</a></dt> <dd><em class="refcount">Return value: Always NULL.</em><p>This function sets the error indicator and returns <em>NULL</em>. <em>exception</em> should be a Python exception (class, not an instance). <em>format</em> should be a string, containing format codes, similar to <tt class="xref docutils literal"><span class="pre">printf()</span></tt>. The <tt class="docutils literal"><span class="pre">width.precision</span></tt> before a format code is parsed, but the width part is ignored.</p> <table border="1" class="docutils"> <colgroup> <col width="29%" /> <col width="23%" /> <col width="48%" /> </colgroup> <thead valign="bottom"> <tr><th class="head">Format Characters</th> <th class="head">Type</th> <th class="head">Comment</th> </tr> </thead> <tbody valign="top"> <tr><td><tt class="xref docutils literal"><span class="pre">%%</span></tt></td> <td><em>n/a</em></td> <td>The literal % character.</td> </tr> <tr><td><tt class="xref docutils literal"><span class="pre">%c</span></tt></td> <td>int</td> <td>A single character, represented as an C int.</td> </tr> <tr><td><tt class="xref docutils literal"><span class="pre">%d</span></tt></td> <td>int</td> <td>Exactly equivalent to <tt class="docutils literal"><span class="pre">printf("%d")</span></tt>.</td> </tr> <tr><td><tt class="xref docutils literal"><span class="pre">%u</span></tt></td> <td>unsigned int</td> <td>Exactly equivalent to <tt class="docutils literal"><span class="pre">printf("%u")</span></tt>.</td> </tr> <tr><td><tt class="xref docutils literal"><span class="pre">%ld</span></tt></td> <td>long</td> <td>Exactly equivalent to <tt class="docutils literal"><span class="pre">printf("%ld")</span></tt>.</td> </tr> <tr><td><tt class="xref docutils literal"><span class="pre">%lu</span></tt></td> <td>unsigned long</td> <td>Exactly equivalent to <tt class="docutils literal"><span class="pre">printf("%lu")</span></tt>.</td> </tr> <tr><td><tt class="xref docutils literal"><span class="pre">%zd</span></tt></td> <td>Py_ssize_t</td> <td>Exactly equivalent to <tt class="docutils literal"><span class="pre">printf("%zd")</span></tt>.</td> </tr> <tr><td><tt class="xref docutils literal"><span class="pre">%zu</span></tt></td> <td>size_t</td> <td>Exactly equivalent to <tt class="docutils literal"><span class="pre">printf("%zu")</span></tt>.</td> </tr> <tr><td><tt class="xref docutils literal"><span class="pre">%i</span></tt></td> <td>int</td> <td>Exactly equivalent to <tt class="docutils literal"><span class="pre">printf("%i")</span></tt>.</td> </tr> <tr><td><tt class="xref docutils literal"><span class="pre">%x</span></tt></td> <td>int</td> <td>Exactly equivalent to <tt class="docutils literal"><span class="pre">printf("%x")</span></tt>.</td> </tr> <tr><td><tt class="xref docutils literal"><span class="pre">%s</span></tt></td> <td>char*</td> <td>A null-terminated C character array.</td> </tr> <tr><td><tt class="xref docutils literal"><span class="pre">%p</span></tt></td> <td>void*</td> <td>The hex representation of a C pointer. Mostly equivalent to <tt class="docutils literal"><span class="pre">printf("%p")</span></tt> except that it is guaranteed to start with the literal <tt class="docutils literal"><span class="pre">0x</span></tt> regardless of what the platform’s <tt class="docutils literal"><span class="pre">printf</span></tt> yields.</td> </tr> </tbody> </table> <p>An unrecognized format character causes all the rest of the format string to be copied as-is to the result string, and any extra arguments discarded.</p> </dd></dl> <dl class="cfunction"> <dt id="PyErr_SetNone"> void <tt class="descname">PyErr_SetNone</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *type</em><big>)</big><a class="headerlink" href="#PyErr_SetNone" title="Permalink to this definition">¶</a></dt> <dd>This is a shorthand for <tt class="docutils literal"><span class="pre">PyErr_SetObject(type,</span> <span class="pre">Py_None)</span></tt>.</dd></dl> <dl class="cfunction"> <dt id="PyErr_BadArgument"> int <tt class="descname">PyErr_BadArgument</tt><big>(</big><big>)</big><a class="headerlink" href="#PyErr_BadArgument" title="Permalink to this definition">¶</a></dt> <dd>This is a shorthand for <tt class="docutils literal"><span class="pre">PyErr_SetString(PyExc_TypeError,</span> <span class="pre">message)</span></tt>, where <em>message</em> indicates that a built-in operation was invoked with an illegal argument. It is mostly for internal use.</dd></dl> <dl class="cfunction"> <dt id="PyErr_NoMemory"> <a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyErr_NoMemory</tt><big>(</big><big>)</big><a class="headerlink" href="#PyErr_NoMemory" title="Permalink to this definition">¶</a></dt> <dd><em class="refcount">Return value: Always NULL.</em><p>This is a shorthand for <tt class="docutils literal"><span class="pre">PyErr_SetNone(PyExc_MemoryError)</span></tt>; it returns <em>NULL</em> so an object allocation function can write <tt class="docutils literal"><span class="pre">return</span> <span class="pre">PyErr_NoMemory();</span></tt> when it runs out of memory.</p> </dd></dl> <dl class="cfunction"> <dt id="PyErr_SetFromErrno"> <a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyErr_SetFromErrno</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *type</em><big>)</big><a class="headerlink" href="#PyErr_SetFromErrno" title="Permalink to this definition">¶</a></dt> <dd><em class="refcount">Return value: Always NULL.</em><p id="index-17">This is a convenience function to raise an exception when a C library function has returned an error and set the C variable <tt class="xref docutils literal"><span class="pre">errno</span></tt>. It constructs a tuple object whose first item is the integer <tt class="xref docutils literal"><span class="pre">errno</span></tt> value and whose second item is the corresponding error message (gotten from <tt class="xref docutils literal"><span class="pre">strerror()</span></tt>), and then calls <tt class="docutils literal"><span class="pre">PyErr_SetObject(type,</span> <span class="pre">object)</span></tt>. On Unix, when the <tt class="xref docutils literal"><span class="pre">errno</span></tt> value is <tt class="xref docutils literal"><span class="pre">EINTR</span></tt>, indicating an interrupted system call, this calls <a title="PyErr_CheckSignals" class="reference internal" href="#PyErr_CheckSignals"><tt class="xref docutils literal"><span class="pre">PyErr_CheckSignals()</span></tt></a>, and if that set the error indicator, leaves it set to that. The function always returns <em>NULL</em>, so a wrapper function around a system call can write <tt class="docutils literal"><span class="pre">return</span> <span class="pre">PyErr_SetFromErrno(type);</span></tt> when the system call returns an error.</p> </dd></dl> <dl class="cfunction"> <dt id="PyErr_SetFromErrnoWithFilename"> <a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyErr_SetFromErrnoWithFilename</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *type</em>, const char<em> *filename</em><big>)</big><a class="headerlink" href="#PyErr_SetFromErrnoWithFilename" title="Permalink to this definition">¶</a></dt> <dd><em class="refcount">Return value: Always NULL.</em><p>Similar to <a title="PyErr_SetFromErrno" class="reference internal" href="#PyErr_SetFromErrno"><tt class="xref docutils literal"><span class="pre">PyErr_SetFromErrno()</span></tt></a>, with the additional behavior that if <em>filename</em> is not <em>NULL</em>, it is passed to the constructor of <em>type</em> as a third parameter. In the case of exceptions such as <a title="exceptions.IOError" class="reference external" href="../library/exceptions.html#exceptions.IOError"><tt class="xref docutils literal"><span class="pre">IOError</span></tt></a> and <a title="exceptions.OSError" class="reference external" href="../library/exceptions.html#exceptions.OSError"><tt class="xref docutils literal"><span class="pre">OSError</span></tt></a>, this is used to define the <tt class="xref docutils literal"><span class="pre">filename</span></tt> attribute of the exception instance.</p> </dd></dl> <dl class="cfunction"> <dt id="PyErr_SetFromWindowsErr"> <a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyErr_SetFromWindowsErr</tt><big>(</big>int<em> ierr</em><big>)</big><a class="headerlink" href="#PyErr_SetFromWindowsErr" title="Permalink to this definition">¶</a></dt> <dd><em class="refcount">Return value: Always NULL.</em><p>This is a convenience function to raise <a title="exceptions.WindowsError" class="reference external" href="../library/exceptions.html#exceptions.WindowsError"><tt class="xref docutils literal"><span class="pre">WindowsError</span></tt></a>. If called with <em>ierr</em> of <tt class="xref docutils literal"><span class="pre">0</span></tt>, the error code returned by a call to <tt class="xref docutils literal"><span class="pre">GetLastError()</span></tt> is used instead. It calls the Win32 function <tt class="xref docutils literal"><span class="pre">FormatMessage()</span></tt> to retrieve the Windows description of error code given by <em>ierr</em> or <tt class="xref docutils literal"><span class="pre">GetLastError()</span></tt>, then it constructs a tuple object whose first item is the <em>ierr</em> value and whose second item is the corresponding error message (gotten from <tt class="xref docutils literal"><span class="pre">FormatMessage()</span></tt>), and then calls <tt class="docutils literal"><span class="pre">PyErr_SetObject(PyExc_WindowsError,</span> <span class="pre">object)</span></tt>. This function always returns <em>NULL</em>. Availability: Windows.</p> </dd></dl> <dl class="cfunction"> <dt id="PyErr_SetExcFromWindowsErr"> <a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyErr_SetExcFromWindowsErr</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *type</em>, int<em> ierr</em><big>)</big><a class="headerlink" href="#PyErr_SetExcFromWindowsErr" title="Permalink to this definition">¶</a></dt> <dd><em class="refcount">Return value: Always NULL.</em><p>Similar to <a title="PyErr_SetFromWindowsErr" class="reference internal" href="#PyErr_SetFromWindowsErr"><tt class="xref docutils literal"><span class="pre">PyErr_SetFromWindowsErr()</span></tt></a>, with an additional parameter specifying the exception type to be raised. Availability: Windows.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.3.</span></p> </dd></dl> <dl class="cfunction"> <dt id="PyErr_SetFromWindowsErrWithFilename"> <a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyErr_SetFromWindowsErrWithFilename</tt><big>(</big>int<em> ierr</em>, const char<em> *filename</em><big>)</big><a class="headerlink" href="#PyErr_SetFromWindowsErrWithFilename" title="Permalink to this definition">¶</a></dt> <dd><em class="refcount">Return value: Always NULL.</em><p>Similar to <a title="PyErr_SetFromWindowsErr" class="reference internal" href="#PyErr_SetFromWindowsErr"><tt class="xref docutils literal"><span class="pre">PyErr_SetFromWindowsErr()</span></tt></a>, with the additional behavior that if <em>filename</em> is not <em>NULL</em>, it is passed to the constructor of <a title="exceptions.WindowsError" class="reference external" href="../library/exceptions.html#exceptions.WindowsError"><tt class="xref docutils literal"><span class="pre">WindowsError</span></tt></a> as a third parameter. Availability: Windows.</p> </dd></dl> <dl class="cfunction"> <dt id="PyErr_SetExcFromWindowsErrWithFilename"> <a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyErr_SetExcFromWindowsErrWithFilename</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *type</em>, int<em> ierr</em>, char<em> *filename</em><big>)</big><a class="headerlink" href="#PyErr_SetExcFromWindowsErrWithFilename" title="Permalink to this definition">¶</a></dt> <dd><em class="refcount">Return value: Always NULL.</em><p>Similar to <a title="PyErr_SetFromWindowsErrWithFilename" class="reference internal" href="#PyErr_SetFromWindowsErrWithFilename"><tt class="xref docutils literal"><span class="pre">PyErr_SetFromWindowsErrWithFilename()</span></tt></a>, with an additional parameter specifying the exception type to be raised. Availability: Windows.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.3.</span></p> </dd></dl> <dl class="cfunction"> <dt id="PyErr_BadInternalCall"> void <tt class="descname">PyErr_BadInternalCall</tt><big>(</big><big>)</big><a class="headerlink" href="#PyErr_BadInternalCall" title="Permalink to this definition">¶</a></dt> <dd>This is a shorthand for <tt class="docutils literal"><span class="pre">PyErr_SetString(PyExc_SystemError,</span> <span class="pre">message)</span></tt>, where <em>message</em> indicates that an internal operation (e.g. a Python/C API function) was invoked with an illegal argument. It is mostly for internal use.</dd></dl> <dl class="cfunction"> <dt id="PyErr_WarnEx"> int <tt class="descname">PyErr_WarnEx</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *category</em>, char<em> *message</em>, int<em> stacklevel</em><big>)</big><a class="headerlink" href="#PyErr_WarnEx" title="Permalink to this definition">¶</a></dt> <dd><p>Issue a warning message. The <em>category</em> argument is a warning category (see below) or <em>NULL</em>; the <em>message</em> argument is a message string. <em>stacklevel</em> is a positive number giving a number of stack frames; the warning will be issued from the currently executing line of code in that stack frame. A <em>stacklevel</em> of 1 is the function calling <a title="PyErr_WarnEx" class="reference internal" href="#PyErr_WarnEx"><tt class="xref docutils literal"><span class="pre">PyErr_WarnEx()</span></tt></a>, 2 is the function above that, and so forth.</p> <p>This function normally prints a warning message to <em>sys.stderr</em>; however, it is also possible that the user has specified that warnings are to be turned into errors, and in that case this will raise an exception. It is also possible that the function raises an exception because of a problem with the warning machinery (the implementation imports the <a title="Issue warning messages and control their disposition." class="reference external" href="../library/warnings.html#module-warnings"><tt class="xref docutils literal"><span class="pre">warnings</span></tt></a> module to do the heavy lifting). The return value is <tt class="docutils literal"><span class="pre">0</span></tt> if no exception is raised, or <tt class="docutils literal"><span class="pre">-1</span></tt> if an exception is raised. (It is not possible to determine whether a warning message is actually printed, nor what the reason is for the exception; this is intentional.) If an exception is raised, the caller should do its normal exception handling (for example, <a title="Py_DECREF" class="reference external" href="refcounting.html#Py_DECREF"><tt class="xref docutils literal"><span class="pre">Py_DECREF()</span></tt></a> owned references and return an error value).</p> <p>Warning categories must be subclasses of <tt class="xref docutils literal"><span class="pre">Warning</span></tt>; the default warning category is <tt class="xref docutils literal"><span class="pre">RuntimeWarning</span></tt>. The standard Python warning categories are available as global variables whose names are <tt class="docutils literal"><span class="pre">PyExc_</span></tt> followed by the Python exception name. These have the type <a title="PyObject" class="reference external" href="structures.html#PyObject"><tt class="xref docutils literal"><span class="pre">PyObject*</span></tt></a>; they are all class objects. Their names are <tt class="xref docutils literal"><span class="pre">PyExc_Warning</span></tt>, <tt class="xref docutils literal"><span class="pre">PyExc_UserWarning</span></tt>, <tt class="xref docutils literal"><span class="pre">PyExc_UnicodeWarning</span></tt>, <tt class="xref docutils literal"><span class="pre">PyExc_DeprecationWarning</span></tt>, <tt class="xref docutils literal"><span class="pre">PyExc_SyntaxWarning</span></tt>, <tt class="xref docutils literal"><span class="pre">PyExc_RuntimeWarning</span></tt>, and <tt class="xref docutils literal"><span class="pre">PyExc_FutureWarning</span></tt>. <tt class="xref docutils literal"><span class="pre">PyExc_Warning</span></tt> is a subclass of <tt class="xref docutils literal"><span class="pre">PyExc_Exception</span></tt>; the other warning categories are subclasses of <tt class="xref docutils literal"><span class="pre">PyExc_Warning</span></tt>.</p> <p>For information about warning control, see the documentation for the <a title="Issue warning messages and control their disposition." class="reference external" href="../library/warnings.html#module-warnings"><tt class="xref docutils literal"><span class="pre">warnings</span></tt></a> module and the <a class="reference external" href="../using/cmdline.html#cmdoption-W"><em class="xref">-W</em></a> option in the command line documentation. There is no C API for warning control.</p> </dd></dl> <dl class="cfunction"> <dt id="PyErr_Warn"> int <tt class="descname">PyErr_Warn</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *category</em>, char<em> *message</em><big>)</big><a class="headerlink" href="#PyErr_Warn" title="Permalink to this definition">¶</a></dt> <dd><p>Issue a warning message. The <em>category</em> argument is a warning category (see below) or <em>NULL</em>; the <em>message</em> argument is a message string. The warning will appear to be issued from the function calling <a title="PyErr_Warn" class="reference internal" href="#PyErr_Warn"><tt class="xref docutils literal"><span class="pre">PyErr_Warn()</span></tt></a>, equivalent to calling <a title="PyErr_WarnEx" class="reference internal" href="#PyErr_WarnEx"><tt class="xref docutils literal"><span class="pre">PyErr_WarnEx()</span></tt></a> with a <em>stacklevel</em> of 1.</p> <p>Deprecated; use <a title="PyErr_WarnEx" class="reference internal" href="#PyErr_WarnEx"><tt class="xref docutils literal"><span class="pre">PyErr_WarnEx()</span></tt></a> instead.</p> </dd></dl> <dl class="cfunction"> <dt id="PyErr_WarnExplicit"> int <tt class="descname">PyErr_WarnExplicit</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *category</em>, const char<em> *message</em>, const char<em> *filename</em>, int<em> lineno</em>, const char<em> *module</em>, <a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *registry</em><big>)</big><a class="headerlink" href="#PyErr_WarnExplicit" title="Permalink to this definition">¶</a></dt> <dd>Issue a warning message with explicit control over all warning attributes. This is a straightforward wrapper around the Python function <a title="warnings.warn_explicit" class="reference external" href="../library/warnings.html#warnings.warn_explicit"><tt class="xref docutils literal"><span class="pre">warnings.warn_explicit()</span></tt></a>, see there for more information. The <em>module</em> and <em>registry</em> arguments may be set to <em>NULL</em> to get the default effect described there.</dd></dl> <dl class="cfunction"> <dt id="PyErr_WarnPy3k"> int <tt class="descname">PyErr_WarnPy3k</tt><big>(</big>char<em> *message</em>, int<em> stacklevel</em><big>)</big><a class="headerlink" href="#PyErr_WarnPy3k" title="Permalink to this definition">¶</a></dt> <dd><p>Issue a <a title="exceptions.DeprecationWarning" class="reference external" href="../library/exceptions.html#exceptions.DeprecationWarning"><tt class="xref docutils literal"><span class="pre">DeprecationWarning</span></tt></a> with the given <em>message</em> and <em>stacklevel</em> if the <tt class="xref docutils literal"><span class="pre">Py_Py3kWarningFlag</span></tt> flag is enabled.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.6.</span></p> </dd></dl> <dl class="cfunction"> <dt id="PyErr_CheckSignals"> int <tt class="descname">PyErr_CheckSignals</tt><big>(</big><big>)</big><a class="headerlink" href="#PyErr_CheckSignals" title="Permalink to this definition">¶</a></dt> <dd><p id="index-18">This function interacts with Python’s signal handling. It checks whether a signal has been sent to the processes and if so, invokes the corresponding signal handler. If the <a title="Set handlers for asynchronous events." class="reference external" href="../library/signal.html#module-signal"><tt class="xref docutils literal"><span class="pre">signal</span></tt></a> module is supported, this can invoke a signal handler written in Python. In all cases, the default effect for <tt class="xref docutils literal"><span class="pre">SIGINT</span></tt> is to raise the <a title="exceptions.KeyboardInterrupt" class="reference external" href="../library/exceptions.html#exceptions.KeyboardInterrupt"><tt class="xref docutils literal"><span class="pre">KeyboardInterrupt</span></tt></a> exception. If an exception is raised the error indicator is set and the function returns <tt class="docutils literal"><span class="pre">-1</span></tt>; otherwise the function returns <tt class="docutils literal"><span class="pre">0</span></tt>. The error indicator may or may not be cleared if it was previously set.</p> </dd></dl> <dl class="cfunction"> <dt id="PyErr_SetInterrupt"> void <tt class="descname">PyErr_SetInterrupt</tt><big>(</big><big>)</big><a class="headerlink" href="#PyErr_SetInterrupt" title="Permalink to this definition">¶</a></dt> <dd><p id="index-19">This function simulates the effect of a <tt class="xref docutils literal"><span class="pre">SIGINT</span></tt> signal arriving — the next time <a title="PyErr_CheckSignals" class="reference internal" href="#PyErr_CheckSignals"><tt class="xref docutils literal"><span class="pre">PyErr_CheckSignals()</span></tt></a> is called, <a title="exceptions.KeyboardInterrupt" class="reference external" href="../library/exceptions.html#exceptions.KeyboardInterrupt"><tt class="xref docutils literal"><span class="pre">KeyboardInterrupt</span></tt></a> will be raised. It may be called without holding the interpreter lock.</p> </dd></dl> <dl class="cfunction"> <dt id="PySignal_SetWakeupFd"> int <tt class="descname">PySignal_SetWakeupFd</tt><big>(</big>int<em> fd</em><big>)</big><a class="headerlink" href="#PySignal_SetWakeupFd" title="Permalink to this definition">¶</a></dt> <dd>This utility function specifies a file descriptor to which a <tt class="docutils literal"><span class="pre">'\0'</span></tt> byte will be written whenever a signal is received. It returns the previous such file descriptor. The value <tt class="docutils literal"><span class="pre">-1</span></tt> disables the feature; this is the initial state. This is equivalent to <a title="signal.set_wakeup_fd" class="reference external" href="../library/signal.html#signal.set_wakeup_fd"><tt class="xref docutils literal"><span class="pre">signal.set_wakeup_fd()</span></tt></a> in Python, but without any error checking. <em>fd</em> should be a valid file descriptor. The function should only be called from the main thread.</dd></dl> <dl class="cfunction"> <dt id="PyErr_NewException"> <a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyErr_NewException</tt><big>(</big>char<em> *name</em>, <a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *base</em>, <a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *dict</em><big>)</big><a class="headerlink" href="#PyErr_NewException" title="Permalink to this definition">¶</a></dt> <dd><em class="refcount">Return value: New reference.</em><p>This utility function creates and returns a new exception object. The <em>name</em> argument must be the name of the new exception, a C string of the form <tt class="docutils literal"><span class="pre">module.class</span></tt>. The <em>base</em> and <em>dict</em> arguments are normally <em>NULL</em>. This creates a class object derived from <a title="exceptions.Exception" class="reference external" href="../library/exceptions.html#exceptions.Exception"><tt class="xref docutils literal"><span class="pre">Exception</span></tt></a> (accessible in C as <tt class="xref docutils literal"><span class="pre">PyExc_Exception</span></tt>).</p> <p>The <tt class="xref docutils literal"><span class="pre">__module__</span></tt> attribute of the new class is set to the first part (up to the last dot) of the <em>name</em> argument, and the class name is set to the last part (after the last dot). The <em>base</em> argument can be used to specify alternate base classes; it can either be only one class or a tuple of classes. The <em>dict</em> argument can be used to specify a dictionary of class variables and methods.</p> </dd></dl> <dl class="cfunction"> <dt id="PyErr_WriteUnraisable"> void <tt class="descname">PyErr_WriteUnraisable</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *obj</em><big>)</big><a class="headerlink" href="#PyErr_WriteUnraisable" title="Permalink to this definition">¶</a></dt> <dd><p>This utility function prints a warning message to <tt class="docutils literal"><span class="pre">sys.stderr</span></tt> when an exception has been set but it is impossible for the interpreter to actually raise the exception. It is used, for example, when an exception occurs in an <a title="object.__del__" class="reference external" href="../reference/datamodel.html#object.__del__"><tt class="xref docutils literal"><span class="pre">__del__()</span></tt></a> method.</p> <p>The function is called with a single argument <em>obj</em> that identifies the context in which the unraisable exception occurred. The repr of <em>obj</em> will be printed in the warning message.</p> </dd></dl> <div class="section" id="standard-exceptions"> <span id="standardexceptions"></span><h2>Standard Exceptions<a class="headerlink" href="#standard-exceptions" title="Permalink to this headline">¶</a></h2> <p>All standard Python exceptions are available as global variables whose names are <tt class="docutils literal"><span class="pre">PyExc_</span></tt> followed by the Python exception name. These have the type <a title="PyObject" class="reference external" href="structures.html#PyObject"><tt class="xref docutils literal"><span class="pre">PyObject*</span></tt></a>; they are all class objects. For completeness, here are all the variables:</p> <table border="1" class="docutils"> <colgroup> <col width="49%" /> <col width="38%" /> <col width="14%" /> </colgroup> <thead valign="bottom"> <tr><th class="head">C Name</th> <th class="head">Python Name</th> <th class="head">Notes</th> </tr> </thead> <tbody valign="top"> <tr><td><tt class="xref docutils literal"><span class="pre">PyExc_BaseException</span></tt></td> <td><a title="exceptions.BaseException" class="reference external" href="../library/exceptions.html#exceptions.BaseException"><tt class="xref docutils literal"><span class="pre">BaseException</span></tt></a></td> <td>(1), (4)</td> </tr> <tr><td><tt class="xref docutils literal"><span class="pre">PyExc_Exception</span></tt></td> <td><a title="exceptions.Exception" class="reference external" href="../library/exceptions.html#exceptions.Exception"><tt class="xref docutils literal"><span class="pre">Exception</span></tt></a></td> <td>(1)</td> </tr> <tr><td><tt class="xref docutils literal"><span class="pre">PyExc_StandardError</span></tt></td> <td><a title="exceptions.StandardError" class="reference external" href="../library/exceptions.html#exceptions.StandardError"><tt class="xref docutils literal"><span class="pre">StandardError</span></tt></a></td> <td>(1)</td> </tr> <tr><td><tt class="xref docutils literal"><span class="pre">PyExc_ArithmeticError</span></tt></td> <td><a title="exceptions.ArithmeticError" class="reference external" href="../library/exceptions.html#exceptions.ArithmeticError"><tt class="xref docutils literal"><span class="pre">ArithmeticError</span></tt></a></td> <td>(1)</td> </tr> <tr><td><tt class="xref docutils literal"><span class="pre">PyExc_LookupError</span></tt></td> <td><a title="exceptions.LookupError" class="reference external" href="../library/exceptions.html#exceptions.LookupError"><tt class="xref docutils literal"><span class="pre">LookupError</span></tt></a></td> <td>(1)</td> </tr> <tr><td><tt class="xref docutils literal"><span class="pre">PyExc_AssertionError</span></tt></td> <td><a title="exceptions.AssertionError" class="reference external" href="../library/exceptions.html#exceptions.AssertionError"><tt class="xref docutils literal"><span class="pre">AssertionError</span></tt></a></td> <td> </td> </tr> <tr><td><tt class="xref docutils literal"><span class="pre">PyExc_AttributeError</span></tt></td> <td><a title="exceptions.AttributeError" class="reference external" href="../library/exceptions.html#exceptions.AttributeError"><tt class="xref docutils literal"><span class="pre">AttributeError</span></tt></a></td> <td> </td> </tr> <tr><td><tt class="xref docutils literal"><span class="pre">PyExc_EOFError</span></tt></td> <td><a title="exceptions.EOFError" class="reference external" href="../library/exceptions.html#exceptions.EOFError"><tt class="xref docutils literal"><span class="pre">EOFError</span></tt></a></td> <td> </td> </tr> <tr><td><tt class="xref docutils literal"><span class="pre">PyExc_EnvironmentError</span></tt></td> <td><a title="exceptions.EnvironmentError" class="reference external" href="../library/exceptions.html#exceptions.EnvironmentError"><tt class="xref docutils literal"><span class="pre">EnvironmentError</span></tt></a></td> <td>(1)</td> </tr> <tr><td><tt class="xref docutils literal"><span class="pre">PyExc_FloatingPointError</span></tt></td> <td><a title="exceptions.FloatingPointError" class="reference external" href="../library/exceptions.html#exceptions.FloatingPointError"><tt class="xref docutils literal"><span class="pre">FloatingPointError</span></tt></a></td> <td> </td> </tr> <tr><td><tt class="xref docutils literal"><span class="pre">PyExc_IOError</span></tt></td> <td><a title="exceptions.IOError" class="reference external" href="../library/exceptions.html#exceptions.IOError"><tt class="xref docutils literal"><span class="pre">IOError</span></tt></a></td> <td> </td> </tr> <tr><td><tt class="xref docutils literal"><span class="pre">PyExc_ImportError</span></tt></td> <td><a title="exceptions.ImportError" class="reference external" href="../library/exceptions.html#exceptions.ImportError"><tt class="xref docutils literal"><span class="pre">ImportError</span></tt></a></td> <td> </td> </tr> <tr><td><tt class="xref docutils literal"><span class="pre">PyExc_IndexError</span></tt></td> <td><a title="exceptions.IndexError" class="reference external" href="../library/exceptions.html#exceptions.IndexError"><tt class="xref docutils literal"><span class="pre">IndexError</span></tt></a></td> <td> </td> </tr> <tr><td><tt class="xref docutils literal"><span class="pre">PyExc_KeyError</span></tt></td> <td><a title="exceptions.KeyError" class="reference external" href="../library/exceptions.html#exceptions.KeyError"><tt class="xref docutils literal"><span class="pre">KeyError</span></tt></a></td> <td> </td> </tr> <tr><td><tt class="xref docutils literal"><span class="pre">PyExc_KeyboardInterrupt</span></tt></td> <td><a title="exceptions.KeyboardInterrupt" class="reference external" href="../library/exceptions.html#exceptions.KeyboardInterrupt"><tt class="xref docutils literal"><span class="pre">KeyboardInterrupt</span></tt></a></td> <td> </td> </tr> <tr><td><tt class="xref docutils literal"><span class="pre">PyExc_MemoryError</span></tt></td> <td><a title="exceptions.MemoryError" class="reference external" href="../library/exceptions.html#exceptions.MemoryError"><tt class="xref docutils literal"><span class="pre">MemoryError</span></tt></a></td> <td> </td> </tr> <tr><td><tt class="xref docutils literal"><span class="pre">PyExc_NameError</span></tt></td> <td><a title="exceptions.NameError" class="reference external" href="../library/exceptions.html#exceptions.NameError"><tt class="xref docutils literal"><span class="pre">NameError</span></tt></a></td> <td> </td> </tr> <tr><td><tt class="xref docutils literal"><span class="pre">PyExc_NotImplementedError</span></tt></td> <td><a title="exceptions.NotImplementedError" class="reference external" href="../library/exceptions.html#exceptions.NotImplementedError"><tt class="xref docutils literal"><span class="pre">NotImplementedError</span></tt></a></td> <td> </td> </tr> <tr><td><tt class="xref docutils literal"><span class="pre">PyExc_OSError</span></tt></td> <td><a title="exceptions.OSError" class="reference external" href="../library/exceptions.html#exceptions.OSError"><tt class="xref docutils literal"><span class="pre">OSError</span></tt></a></td> <td> </td> </tr> <tr><td><tt class="xref docutils literal"><span class="pre">PyExc_OverflowError</span></tt></td> <td><a title="exceptions.OverflowError" class="reference external" href="../library/exceptions.html#exceptions.OverflowError"><tt class="xref docutils literal"><span class="pre">OverflowError</span></tt></a></td> <td> </td> </tr> <tr><td><tt class="xref docutils literal"><span class="pre">PyExc_ReferenceError</span></tt></td> <td><a title="exceptions.ReferenceError" class="reference external" href="../library/exceptions.html#exceptions.ReferenceError"><tt class="xref docutils literal"><span class="pre">ReferenceError</span></tt></a></td> <td>(2)</td> </tr> <tr><td><tt class="xref docutils literal"><span class="pre">PyExc_RuntimeError</span></tt></td> <td><a title="exceptions.RuntimeError" class="reference external" href="../library/exceptions.html#exceptions.RuntimeError"><tt class="xref docutils literal"><span class="pre">RuntimeError</span></tt></a></td> <td> </td> </tr> <tr><td><tt class="xref docutils literal"><span class="pre">PyExc_SyntaxError</span></tt></td> <td><a title="exceptions.SyntaxError" class="reference external" href="../library/exceptions.html#exceptions.SyntaxError"><tt class="xref docutils literal"><span class="pre">SyntaxError</span></tt></a></td> <td> </td> </tr> <tr><td><tt class="xref docutils literal"><span class="pre">PyExc_SystemError</span></tt></td> <td><a title="exceptions.SystemError" class="reference external" href="../library/exceptions.html#exceptions.SystemError"><tt class="xref docutils literal"><span class="pre">SystemError</span></tt></a></td> <td> </td> </tr> <tr><td><tt class="xref docutils literal"><span class="pre">PyExc_SystemExit</span></tt></td> <td><a title="exceptions.SystemExit" class="reference external" href="../library/exceptions.html#exceptions.SystemExit"><tt class="xref docutils literal"><span class="pre">SystemExit</span></tt></a></td> <td> </td> </tr> <tr><td><tt class="xref docutils literal"><span class="pre">PyExc_TypeError</span></tt></td> <td><a title="exceptions.TypeError" class="reference external" href="../library/exceptions.html#exceptions.TypeError"><tt class="xref docutils literal"><span class="pre">TypeError</span></tt></a></td> <td> </td> </tr> <tr><td><tt class="xref docutils literal"><span class="pre">PyExc_ValueError</span></tt></td> <td><a title="exceptions.ValueError" class="reference external" href="../library/exceptions.html#exceptions.ValueError"><tt class="xref docutils literal"><span class="pre">ValueError</span></tt></a></td> <td> </td> </tr> <tr><td><tt class="xref docutils literal"><span class="pre">PyExc_WindowsError</span></tt></td> <td><a title="exceptions.WindowsError" class="reference external" href="../library/exceptions.html#exceptions.WindowsError"><tt class="xref docutils literal"><span class="pre">WindowsError</span></tt></a></td> <td>(3)</td> </tr> <tr><td><tt class="xref docutils literal"><span class="pre">PyExc_ZeroDivisionError</span></tt></td> <td><a title="exceptions.ZeroDivisionError" class="reference external" href="../library/exceptions.html#exceptions.ZeroDivisionError"><tt class="xref docutils literal"><span class="pre">ZeroDivisionError</span></tt></a></td> <td> </td> </tr> </tbody> </table> <p id="index-20">Notes:</p> <ol class="arabic"> <li><p class="first">This is a base class for other standard exceptions.</p> </li> <li><p class="first">This is the same as <a title="weakref.ReferenceError" class="reference external" href="../library/weakref.html#weakref.ReferenceError"><tt class="xref docutils literal"><span class="pre">weakref.ReferenceError</span></tt></a>.</p> </li> <li><p class="first">Only defined on Windows; protect code that uses this by testing that the preprocessor macro <tt class="docutils literal"><span class="pre">MS_WINDOWS</span></tt> is defined.</p> </li> <li><p class="first versionadded"> <span class="versionmodified">New in version 2.5.</span></p> </li> </ol> </div> <div class="section" id="deprecation-of-string-exceptions"> <h2>Deprecation of String Exceptions<a class="headerlink" href="#deprecation-of-string-exceptions" title="Permalink to this headline">¶</a></h2> <p id="index-21">All exceptions built into Python or provided in the standard library are derived from <a title="exceptions.BaseException" class="reference external" href="../library/exceptions.html#exceptions.BaseException"><tt class="xref docutils literal"><span class="pre">BaseException</span></tt></a>.</p> <p>String exceptions are still supported in the interpreter to allow existing code to run unmodified, but this will also change in a future release.</p> </div> </div> </div> </div> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h3><a href="../contents.html">Table Of Contents</a></h3> <ul> <li><a class="reference external" href="#">Exception Handling</a><ul> <li><a class="reference external" href="#standard-exceptions">Standard Exceptions</a></li> <li><a class="reference external" href="#deprecation-of-string-exceptions">Deprecation of String Exceptions</a></li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="refcounting.html" title="previous chapter">Reference Counting</a></p> <h4>Next topic</h4> <p class="topless"><a href="utilities.html" title="next chapter">Utilities</a></p> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../_sources/c-api/exceptions.txt" rel="nofollow">Show Source</a></li> </ul> <div id="searchbox" style="display: none"> <h3>Quick search</h3> <form class="search" action="../search.html" method="get"> <input type="text" name="q" size="18" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="../modindex.html" title="Global Module Index" >modules</a> |</li> <li class="right" > <a href="utilities.html" title="Utilities" >next</a> |</li> <li class="right" > <a href="refcounting.html" title="Reference Counting" >previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="../index.html">Python v2.6.5 documentation</a> »</li> <li><a href="index.html" >Python/C API Reference Manual</a> »</li> </ul> </div> <div class="footer"> © <a href="../copyright.html">Copyright</a> 1990-2010, Python Software Foundation. <br /> The Python Software Foundation is a non-profit corporation. <a href="http://www.python.org/psf/donations/">Please donate.</a> <br /> Last updated on Mar 19, 2010. Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.5. </div> </body> </html>