Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / amd64 / html / python / api / exceptions.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="api.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index' />
<link rel="first" href="api.html" title='Python/C API Reference Manual' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="embedding.html" />
<link rel="prev" href="objects.html" />
<link rel="parent" href="intro.html" />
<link rel="next" href="embedding.html" />
<meta name='aesop' content='information' />
<title>1.3 Exceptions </title>
</head>
<body>
<DIV CLASS="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="1.2.2 Types"
href="types.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="1. Introduction"
href="intro.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="1.4 Embedding Python"
href="embedding.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python/C API Reference Manual</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><img src='../icons/blank.png'
border='0' height='32' alt='' width='32' /></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="types.html">1.2.2 Types</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="intro.html">1. Introduction</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="embedding.html">1.4 Embedding Python</A>
</div>
<hr /></div>
</DIV>
<!--End of Navigation Panel-->
<H1><A NAME="SECTION003300000000000000000"></A><A NAME="exceptions"></A>
<BR>
1.3 Exceptions
</H1>
<P>
The Python programmer only needs to deal with exceptions if specific
error handling is required; unhandled exceptions are automatically
propagated to the caller, then to the caller's caller, and so on, until
they reach the top-level interpreter, where they are reported to the
user accompanied by a stack traceback.
<P>
For C programmers, however, error checking always has to be explicit.
All functions in the Python/C API can raise exceptions, unless an
explicit claim is made otherwise in a function's documentation. In
general, when a function encounters an error, it sets an exception,
discards any object references that it owns, and returns an
error indicator -- usually <tt class="constant">NULL</tt> or <code>-1</code>. A few functions
return a Boolean true/false result, with false indicating an error.
Very few functions return no explicit error indicator or have an
ambiguous return value, and require explicit testing for errors with
<tt class="cfunction">PyErr_Occurred()</tt><a id='l2h-20' xml:id='l2h-20'></a>.
<P>
Exception state is maintained in per-thread storage (this is
equivalent to using global storage in an unthreaded application). A
thread can be in one of two states: an exception has occurred, or not.
The function <tt class="cfunction">PyErr_Occurred()</tt> can be used to check for
this: it returns a borrowed reference to the exception type object
when an exception has occurred, and <tt class="constant">NULL</tt> otherwise. There are a
number of functions to set the exception state:
<tt class="cfunction">PyErr_SetString()</tt><a id='l2h-21' xml:id='l2h-21'></a> is the most
common (though not the most general) function to set the exception
state, and <tt class="cfunction">PyErr_Clear()</tt><a id='l2h-22' xml:id='l2h-22'></a> clears the
exception state.
<P>
The full exception state consists of three objects (all of which can
be <tt class="constant">NULL</tt>): the exception type, the corresponding exception
value, and the traceback. These have the same meanings as the Python
<a id='l2h-17' xml:id='l2h-17'></a>objects <code>sys.exc_type</code>, <code>sys.exc_value</code>, and
<code>sys.exc_traceback</code>; however, they are not the same: the Python
objects represent the last exception being handled by a Python
<tt class="keyword">try</tt> ... <tt class="keyword">except</tt> statement, while the C level
exception state only exists while an exception is being passed on
between C functions until it reaches the Python bytecode interpreter's
main loop, which takes care of transferring it to <code>sys.exc_type</code>
and friends.
<P>
Note that starting with Python 1.5, the preferred, thread-safe way to
access the exception state from Python code is to call the function
<a id='l2h-19' xml:id='l2h-19'></a><tt class="function">sys.exc_info()</tt>, which returns the per-thread exception state
for Python code. Also, the semantics of both ways to access the
exception state have changed so that a function which catches an
exception will save and restore its thread's exception state so as to
preserve the exception state of its caller. This prevents common bugs
in exception handling code caused by an innocent-looking function
overwriting the exception being handled; it also reduces the often
unwanted lifetime extension for objects that are referenced by the
stack frames in the traceback.
<P>
As a general principle, a function that calls another function to
perform some task should check whether the called function raised an
exception, and if so, pass the exception state on to its caller. It
should discard any object references that it owns, and return an
error indicator, but it should <em>not</em> set another exception --
that would overwrite the exception that was just raised, and lose
important information about the exact cause of the error.
<P>
A simple example of detecting exceptions and passing them on is shown
in the <tt class="cfunction">sum_sequence()</tt><a id='l2h-23' xml:id='l2h-23'></a> example
above. It so happens that that example doesn't need to clean up any
owned references when it detects an error. The following example
function shows some error cleanup. First, to remind you why you like
Python, we show the equivalent Python code:
<P>
<div class="verbatim"><pre>
def incr_item(dict, key):
try:
item = dict[key]
except KeyError:
item = 0
dict[key] = item + 1
</pre></div>
<P>
Here is the corresponding C code, in all its glory:
<P>
<div class="verbatim"><pre>
int
incr_item(PyObject *dict, PyObject *key)
{
/* Objects all initialized to NULL for Py_XDECREF */
PyObject *item = NULL, *const_one = NULL, *incremented_item = NULL;
int rv = -1; /* Return value initialized to -1 (failure) */
item = PyObject_GetItem(dict, key);
if (item == NULL) {
/* Handle KeyError only: */
if (!PyErr_ExceptionMatches(PyExc_KeyError))
goto error;
/* Clear the error and use zero: */
PyErr_Clear();
item = PyInt_FromLong(0L);
if (item == NULL)
goto error;
}
const_one = PyInt_FromLong(1L);
if (const_one == NULL)
goto error;
incremented_item = PyNumber_Add(item, const_one);
if (incremented_item == NULL)
goto error;
if (PyObject_SetItem(dict, key, incremented_item) &lt; 0)
goto error;
rv = 0; /* Success */
/* Continue with cleanup code */
error:
/* Cleanup code, shared by success and failure path */
/* Use Py_XDECREF() to ignore NULL references */
Py_XDECREF(item);
Py_XDECREF(const_one);
Py_XDECREF(incremented_item);
return rv; /* -1 for error, 0 for success */
}
</pre></div>
<P>
This example represents an endorsed use of the <tt class="keyword">goto</tt> statement
in C! It illustrates the use of
<tt class="cfunction">PyErr_ExceptionMatches()</tt><a id='l2h-24' xml:id='l2h-24'></a> and
<tt class="cfunction">PyErr_Clear()</tt><a id='l2h-25' xml:id='l2h-25'></a> to
handle specific exceptions, and the use of
<tt class="cfunction">Py_XDECREF()</tt><a id='l2h-26' xml:id='l2h-26'></a> to
dispose of owned references that may be <tt class="constant">NULL</tt> (note the
"<tt class="character">X</tt>" in the name; <tt class="cfunction">Py_DECREF()</tt> would crash when
confronted with a <tt class="constant">NULL</tt> reference). It is important that the
variables used to hold owned references are initialized to <tt class="constant">NULL</tt> for
this to work; likewise, the proposed return value is initialized to
<code>-1</code> (failure) and only set to success after the final call made
is successful.
<P>
<DIV CLASS="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="1.2.2 Types"
href="types.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="1. Introduction"
href="intro.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="1.4 Embedding Python"
href="embedding.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python/C API Reference Manual</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><img src='../icons/blank.png'
border='0' height='32' alt='' width='32' /></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="types.html">1.2.2 Types</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="intro.html">1. Introduction</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="embedding.html">1.4 Embedding Python</A>
</div>
</div>
<hr />
<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>