| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <link rel="STYLESHEET" href="ref.css" type='text/css' /> |
| 5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> |
| 6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> |
| 7 | <link rel="first" href="ref.html" title='Python Reference Manual' /> |
| 8 | <link rel='contents' href='contents.html' title="Contents" /> |
| 9 | <link rel='index' href='genindex.html' title='Index' /> |
| 10 | <link rel='last' href='about.html' title='About this document...' /> |
| 11 | <link rel='help' href='about.html' title='About this document...' /> |
| 12 | <link rel="next" href="function.html" /> |
| 13 | <link rel="prev" href="for.html" /> |
| 14 | <link rel="parent" href="compound.html" /> |
| 15 | <link rel="next" href="function.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>7.4 The try statement</title> |
| 18 | </head> |
| 19 | <body> |
| 20 | <DIV CLASS="navigation"> |
| 21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 23 | <tr> |
| 24 | <td class='online-navigation'><a rel="prev" title="7.3 The for statement" |
| 25 | href="for.html"><img src='../icons/previous.png' |
| 26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 27 | <td class='online-navigation'><a rel="parent" title="7. Compound statements" |
| 28 | href="compound.html"><img src='../icons/up.png' |
| 29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 30 | <td class='online-navigation'><a rel="next" title="7.5 Function definitions" |
| 31 | href="function.html"><img src='../icons/next.png' |
| 32 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 33 | <td align="center" width="100%">Python Reference Manual</td> |
| 34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 35 | href="contents.html"><img src='../icons/contents.png' |
| 36 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 37 | <td class='online-navigation'><img src='../icons/blank.png' |
| 38 | border='0' height='32' alt='' width='32' /></td> |
| 39 | <td class='online-navigation'><a rel="index" title="Index" |
| 40 | href="genindex.html"><img src='../icons/index.png' |
| 41 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 42 | </tr></table> |
| 43 | <div class='online-navigation'> |
| 44 | <b class="navlabel">Previous:</b> |
| 45 | <a class="sectref" rel="prev" href="for.html">7.3 The for statement</A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="compound.html">7. Compound statements</A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="function.html">7.5 Function definitions</A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H1><A NAME="SECTION009400000000000000000"></A><A NAME="try"></A> |
| 56 | <BR> |
| 57 | 7.4 The <tt class="keyword">try</tt> statement |
| 58 | </H1> |
| 59 | <a id='l2h-592' xml:id='l2h-592'></a> |
| 60 | <P> |
| 61 | The <tt class="keyword">try</tt> statement specifies exception handlers and/or cleanup |
| 62 | code for a group of statements: |
| 63 | |
| 64 | <P> |
| 65 | <dl><dd class="grammar"> |
| 66 | <div class="productions"> |
| 67 | <table> |
| 68 | <tr> |
| 69 | <td><a id='tok-try_stmt' xml:id='tok-try_stmt'>try_stmt</a></td> |
| 70 | <td>::=</td> |
| 71 | <td><a class='grammartoken' href="try.html#tok-try_exc_stmt">try_exc_stmt</a> | <a class='grammartoken' href="try.html#tok-try_fin_stmt">try_fin_stmt</a></td></tr> |
| 72 | <tr> |
| 73 | <td><a id='tok-try_exc_stmt' xml:id='tok-try_exc_stmt'>try_exc_stmt</a></td> |
| 74 | <td>::=</td> |
| 75 | <td>"try" ":" <a class='grammartoken' href="compound.html#tok-suite">suite</a></td></tr> |
| 76 | <tr> |
| 77 | <td></td> |
| 78 | <td></td> |
| 79 | <td><code>("except" [<a class='grammartoken' href="Booleans.html#tok-expression">expression</a> |
| 80 | ["," <a class='grammartoken' href="assignment.html#tok-target">target</a>]] ":" <a class='grammartoken' href="compound.html#tok-suite">suite</a>)+</code></td></tr> |
| 81 | <tr> |
| 82 | <td></td> |
| 83 | <td></td> |
| 84 | <td><code>["else" ":" <a class='grammartoken' href="compound.html#tok-suite">suite</a>]</code></td></tr> |
| 85 | <tr> |
| 86 | <td><a id='tok-try_fin_stmt' xml:id='tok-try_fin_stmt'>try_fin_stmt</a></td> |
| 87 | <td>::=</td> |
| 88 | <td>"try" ":" <a class='grammartoken' href="compound.html#tok-suite">suite</a> |
| 89 | "finally" ":" <a class='grammartoken' href="compound.html#tok-suite">suite</a></td></tr> |
| 90 | </table> |
| 91 | </div> |
| 92 | <a class="grammar-footer" |
| 93 | href="grammar.txt" type="text/plain" |
| 94 | >Download entire grammar as text.</a> |
| 95 | </dd></dl> |
| 96 | |
| 97 | <P> |
| 98 | There are two forms of <tt class="keyword">try</tt> statement: |
| 99 | <tt class="keyword">try</tt>...<tt class="keyword">except</tt> and |
| 100 | <tt class="keyword">try</tt>...<tt class="keyword">finally</tt>. These forms cannot be mixed (but |
| 101 | they can be nested in each other). |
| 102 | |
| 103 | <P> |
| 104 | The <tt class="keyword">try</tt>...<tt class="keyword">except</tt> form specifies one or more |
| 105 | exception handlers |
| 106 | (the <tt class="keyword">except</tt> clauses). When no exception occurs in the |
| 107 | <tt class="keyword">try</tt> clause, no exception handler is executed. When an |
| 108 | exception occurs in the <tt class="keyword">try</tt> suite, a search for an exception |
| 109 | handler is started. This search inspects the except clauses in turn until |
| 110 | one is found that matches the exception. An expression-less except |
| 111 | clause, if present, must be last; it matches any exception. For an |
| 112 | except clause with an expression, that expression is evaluated, and the |
| 113 | clause matches the exception if the resulting object is ``compatible'' |
| 114 | with the exception. An object is compatible with an exception if it |
| 115 | is either the object that identifies the exception, or (for exceptions |
| 116 | that are classes) it is a base class of the exception, or it is a |
| 117 | tuple containing an item that is compatible with the exception. Note |
| 118 | that the object identities must match, i.e. it must be the same |
| 119 | object, not just an object with the same value. |
| 120 | <a id='l2h-593' xml:id='l2h-593'></a> |
| 121 | <P> |
| 122 | If no except clause matches the exception, the search for an exception |
| 123 | handler continues in the surrounding code and on the invocation stack. |
| 124 | |
| 125 | <P> |
| 126 | If the evaluation of an expression in the header of an except clause |
| 127 | raises an exception, the original search for a handler is canceled |
| 128 | and a search starts for the new exception in the surrounding code and |
| 129 | on the call stack (it is treated as if the entire <tt class="keyword">try</tt> statement |
| 130 | raised the exception). |
| 131 | |
| 132 | <P> |
| 133 | When a matching except clause is found, the exception's parameter is |
| 134 | assigned to the target specified in that except clause, if present, |
| 135 | and the except clause's suite is executed. All except clauses must |
| 136 | have an executable block. When the end of this block |
| 137 | is reached, execution continues normally after the entire try |
| 138 | statement. (This means that if two nested handlers exist for the same |
| 139 | exception, and the exception occurs in the try clause of the inner |
| 140 | handler, the outer handler will not handle the exception.) |
| 141 | |
| 142 | <P> |
| 143 | Before an except clause's suite is executed, details about the |
| 144 | exception are assigned to three variables in the |
| 145 | <tt class="module">sys</tt><a id='l2h-605' xml:id='l2h-605'></a> module: <code>sys.exc_type</code> receives |
| 146 | the object identifying the exception; <code>sys.exc_value</code> receives |
| 147 | the exception's parameter; <code>sys.exc_traceback</code> receives a |
| 148 | traceback object<a id='l2h-594' xml:id='l2h-594'></a> (see section <A href="types.html#traceback">3.2</A>) |
| 149 | identifying the point in the program where the exception occurred. |
| 150 | These details are also available through the <tt class="function">sys.exc_info()</tt> |
| 151 | function, which returns a tuple <code>(<var>exc_type</var>, <var>exc_value</var>, |
| 152 | <var>exc_traceback</var>)</code>. Use of the corresponding variables is |
| 153 | deprecated in favor of this function, since their use is unsafe in a |
| 154 | threaded program. As of Python 1.5, the variables are restored to |
| 155 | their previous values (before the call) when returning from a function |
| 156 | that handled an exception. |
| 157 | <a id='l2h-596' xml:id='l2h-596'></a> |
| 158 | <P> |
| 159 | The optional <tt class="keyword">else</tt> clause is executed if and when control |
| 160 | flows off the end of the <tt class="keyword">try</tt> clause.<A NAME="tex2html10" |
| 161 | HREF="#foot6708"><SUP>7.1</SUP></A> Exceptions in the <tt class="keyword">else</tt> clause are not handled by the |
| 162 | preceding <tt class="keyword">except</tt> clauses. |
| 163 | <a id='l2h-597' xml:id='l2h-597'></a><a id='l2h-598' xml:id='l2h-598'></a><a id='l2h-599' xml:id='l2h-599'></a><a id='l2h-600' xml:id='l2h-600'></a> |
| 164 | <P> |
| 165 | The <tt class="keyword">try</tt>...<tt class="keyword">finally</tt> form specifies a `cleanup' handler. The |
| 166 | <tt class="keyword">try</tt> clause is executed. When no exception occurs, the |
| 167 | <tt class="keyword">finally</tt> clause is executed. When an exception occurs in the |
| 168 | <tt class="keyword">try</tt> clause, the exception is temporarily saved, the |
| 169 | <tt class="keyword">finally</tt> clause is executed, and then the saved exception is |
| 170 | re-raised. If the <tt class="keyword">finally</tt> clause raises another exception or |
| 171 | executes a <tt class="keyword">return</tt> or <tt class="keyword">break</tt> statement, the saved |
| 172 | exception is lost. A <tt class="keyword">continue</tt> statement is illegal in the |
| 173 | <tt class="keyword">finally</tt> clause. (The reason is a problem with the current |
| 174 | implementation - this restriction may be lifted in the future). The |
| 175 | exception information is not available to the program during execution of |
| 176 | the <tt class="keyword">finally</tt> clause. |
| 177 | <a id='l2h-601' xml:id='l2h-601'></a> |
| 178 | <P> |
| 179 | When a <tt class="keyword">return</tt>, <tt class="keyword">break</tt> or <tt class="keyword">continue</tt> statement is |
| 180 | executed in the <tt class="keyword">try</tt> suite of a <tt class="keyword">try</tt>...<tt class="keyword">finally</tt> |
| 181 | statement, the <tt class="keyword">finally</tt> clause is also executed `on the way out.' A |
| 182 | <tt class="keyword">continue</tt> statement is illegal in the <tt class="keyword">finally</tt> clause. |
| 183 | (The reason is a problem with the current implementation -- this |
| 184 | restriction may be lifted in the future). |
| 185 | <a id='l2h-602' xml:id='l2h-602'></a><a id='l2h-603' xml:id='l2h-603'></a><a id='l2h-604' xml:id='l2h-604'></a> |
| 186 | <P> |
| 187 | Additional information on exceptions can be found in |
| 188 | section <A href="exceptions.html#exceptions">4.2</A>, and information on using the <tt class="keyword">raise</tt> |
| 189 | statement to generate exceptions may be found in section <A href="raise.html#raise">6.9</A>. |
| 190 | |
| 191 | <P> |
| 192 | <BR><HR><H4>Footnotes</H4> |
| 193 | <DL> |
| 194 | <DT><A NAME="foot6708">... clause.</A><A |
| 195 | href="try.html#tex2html10"><SUP>7.1</SUP></A></DT> |
| 196 | <DD> |
| 197 | Currently, control ``flows off the end'' except in the case of an |
| 198 | exception or the execution of a <tt class="keyword">return</tt>, |
| 199 | <tt class="keyword">continue</tt>, or <tt class="keyword">break</tt> statement. |
| 200 | |
| 201 | |
| 202 | </DD> |
| 203 | </DL> |
| 204 | <DIV CLASS="navigation"> |
| 205 | <div class='online-navigation'> |
| 206 | <p></p><hr /> |
| 207 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 208 | <tr> |
| 209 | <td class='online-navigation'><a rel="prev" title="7.3 The for statement" |
| 210 | href="for.html"><img src='../icons/previous.png' |
| 211 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 212 | <td class='online-navigation'><a rel="parent" title="7. Compound statements" |
| 213 | href="compound.html"><img src='../icons/up.png' |
| 214 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 215 | <td class='online-navigation'><a rel="next" title="7.5 Function definitions" |
| 216 | href="function.html"><img src='../icons/next.png' |
| 217 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 218 | <td align="center" width="100%">Python Reference Manual</td> |
| 219 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 220 | href="contents.html"><img src='../icons/contents.png' |
| 221 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 222 | <td class='online-navigation'><img src='../icons/blank.png' |
| 223 | border='0' height='32' alt='' width='32' /></td> |
| 224 | <td class='online-navigation'><a rel="index" title="Index" |
| 225 | href="genindex.html"><img src='../icons/index.png' |
| 226 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 227 | </tr></table> |
| 228 | <div class='online-navigation'> |
| 229 | <b class="navlabel">Previous:</b> |
| 230 | <a class="sectref" rel="prev" href="for.html">7.3 The for statement</A> |
| 231 | <b class="navlabel">Up:</b> |
| 232 | <a class="sectref" rel="parent" href="compound.html">7. Compound statements</A> |
| 233 | <b class="navlabel">Next:</b> |
| 234 | <a class="sectref" rel="next" href="function.html">7.5 Function definitions</A> |
| 235 | </div> |
| 236 | </div> |
| 237 | <hr /> |
| 238 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 239 | </DIV> |
| 240 | <!--End of Navigation Panel--> |
| 241 | <ADDRESS> |
| 242 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 243 | </ADDRESS> |
| 244 | </BODY> |
| 245 | </HTML> |