<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!--Converted with LaTeX2HTML 98.2 beta6 (August 14th, 1998)
original version by:  Nikos Drakos, CBLU, University of Leeds
* revised and updated by:  Marcus Hennecke, Ross Moore, Herb Swan
* with significant contributions from:
  Jens Lippmann, Marek Rouchal, Martin Wilck and others -->
<HTML>
<HEAD>
<TITLE>7.4 The try statement</TITLE>
<META NAME="description" CONTENT="7.4 The try statement">
<META NAME="keywords" CONTENT="ref">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<LINK REL="STYLESHEET" HREF="ref.css">
<LINK REL="next" HREF="function.html">
<LINK REL="previous" HREF="for.html">
<LINK REL="up" HREF="compound.html">
<LINK REL="next" HREF="function.html">
</HEAD>
<BODY bgcolor="#ffffff">
<div class=navigation>
<table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="function.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="compound.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="for.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Python Reference Manual</b></td>
<td><A HREF="contents.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><img width=32 height=32 align=bottom border=0 alt=""
 src="../icons/blank.gif"></td>
<td><A HREF="genindex.html"><img width=32 height=32 align=bottom border=0 alt="index"
 src="../icons/index.gif"></A></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="function.html">7.5 Function definitions</A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="compound.html">7. Compound statements</A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="for.html">7.3 The for statement</A></span>
<br><hr></div>
<!--End of Navigation Panel-->

<H1><A NAME="SECTION009400000000000000000"></A>
<BR>
7.4 The <tt class=keyword>try</tt> statement
</H1>
<a name="l2h-699"></a>

<P>
The <tt class=keyword>try</tt> statement specifies exception handlers and/or cleanup
code for a group of statements:

<P>
<dl><dd><pre class=verbatim>
try_stmt:       try_exc_stmt | try_fin_stmt
try_exc_stmt:   "try" ":" suite
               ("except" [expression ["," target]] ":" suite)+
               ["else" ":" suite]
try_fin_stmt:   "try" ":" suite
               "finally" ":" suite
</pre></dl>

<P>
There are two forms of <tt class=keyword>try</tt> statement:
<tt class=keyword>try</tt>...<tt class=keyword>except</tt> and
<tt class=keyword>try</tt>...<tt class=keyword>finally</tt>.  These forms cannot be mixed (but
they can be nested in each other).

<P>
The <tt class=keyword>try</tt>...<tt class=keyword>except</tt> form specifies one or more
exception handlers
(the <tt class=keyword>except</tt> clauses).  When no exception occurs in the
<tt class=keyword>try</tt> clause, no exception handler is executed.  When an
exception occurs in the <tt class=keyword>try</tt> suite, a search for an exception
handler is started.  This search inspects the except clauses in turn until
one is found that matches the exception.  An expression-less except
clause, if present, must be last; it matches any exception.  For an
except clause with an expression, that expression is evaluated, and the
clause matches the exception if the resulting object is ``compatible''
with the exception.  An object is compatible with an exception if it
is either the object that identifies the exception, or (for exceptions
that are classes) it is a base class of the exception, or it is a
tuple containing an item that is compatible with the exception.  Note
that the object identities must match, i.e. it must be the same
object, not just an object with the same value.
<a name="l2h-700"></a>

<P>
If no except clause matches the exception, the search for an exception
handler continues in the surrounding code and on the invocation stack.

<P>
If the evaluation of an expression in the header of an except clause
raises an exception, the original search for a handler is cancelled
and a search starts for the new exception in the surrounding code and
on the call stack (it is treated as if the entire <tt class=keyword>try</tt> statement
raised the exception).

<P>
When a matching except clause is found, the exception's parameter is
assigned to the target specified in that except clause, if present,
and the except clause's suite is executed.  When the end of this suite
is reached, execution continues normally after the entire try
statement.  (This means that if two nested handlers exist for the same
exception, and the exception occurs in the try clause of the inner
handler, the outer handler will not handle the exception.)

<P>
Before an except clause's suite is executed, details about the
exception are assigned to three variables in the
<tt class=module>sys</tt><a name="l2h-701"></a> module: <tt>sys.exc_type</tt> receives
the object identifying the exception; <tt>sys.exc_value</tt> receives
the exception's parameter; <tt>sys.exc_traceback</tt> receives a
traceback object<a name="l2h-702"></a> (see section <A HREF="types.html#traceback">3.2</A>)
identifying the point in the program where the exception occurred.
These details are also available through the <tt class=function>sys.exc_info()</tt>
function, which returns a tuple <tt>(<var>exc_type</var>, <var>exc_value</var>,
<var>exc_traceback</var>)</tt>.  Use of the corresponding variables is
deprecated in favor of this function, since their use is unsafe in a
threaded program.  As of Python 1.5, the variables are restored to
their previous values (before the call) when returning from a function
that handled an exception.
<a name="l2h-696"></a>
  <a name="l2h-697"></a><a name="l2h-698"></a>
<P>
The optional <tt class=keyword>else</tt> clause is executed when no exception occurs
in the <tt class=keyword>try</tt> clause.  Exceptions in the <tt class=keyword>else</tt> clause are
not handled by the preceding <tt class=keyword>except</tt> clauses.
<a name="l2h-703"></a>

<P>
The <tt class=keyword>try</tt>...<tt class=keyword>finally</tt> form specifies a `cleanup' handler.  The
<tt class=keyword>try</tt> clause is executed.  When no exception occurs, the
<tt class=keyword>finally</tt> clause is executed.  When an exception occurs in the
<tt class=keyword>try</tt> clause, the exception is temporarily saved, the
<tt class=keyword>finally</tt> clause is executed, and then the saved exception is
re-raised.  If the <tt class=keyword>finally</tt> clause raises another exception or
executes a <tt class=keyword>return</tt>, <tt class=keyword>break</tt> or <tt class=keyword>continue</tt> statement,
the saved exception is lost.  The exception information is not
available to the program during execution of the <tt class=keyword>finally</tt>
clause.
<a name="l2h-704"></a>

<P>
When a <tt class=keyword>return</tt> or <tt class=keyword>break</tt> statement is executed in the
<tt class=keyword>try</tt> suite of a <tt class=keyword>try</tt>...<tt class=keyword>finally</tt> statement, the
<tt class=keyword>finally</tt> clause is also executed `on the way out.'  A
<tt class=keyword>continue</tt> statement is illegal in the <tt class=keyword>try</tt> clause.  (The
reason is a problem with the current implementation -- this
restriction may be lifted in the future).
<a name="l2h-705"></a>
<a name="l2h-706"></a>
<a name="l2h-707"></a>

<P>
<p>
<div class=navigation><hr><table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="function.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="compound.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="for.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Python Reference Manual</b></td>
<td><A HREF="contents.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><img width=32 height=32 align=bottom border=0 alt=""
 src="../icons/blank.gif"></td>
<td><A HREF="genindex.html"><img width=32 height=32 align=bottom border=0 alt="index"
 src="../icons/index.gif"></A></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="function.html">7.5 Function definitions</A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="compound.html">7. Compound statements</A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="for.html">7.3 The for statement</A></span>
</div>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>Send comments on this document to <a href="mailto:python-docs@python.org">python-docs@python.org</a>.
</ADDRESS>
</BODY>
</HTML>
