| 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="prev" href="naming.html" /> |
| 13 | <link rel="parent" href="execmodel.html" /> |
| 14 | <link rel="next" href="expressions.html" /> |
| 15 | <meta name='aesop' content='information' /> |
| 16 | <title>4.2 Exceptions </title> |
| 17 | </head> |
| 18 | <body> |
| 19 | <DIV CLASS="navigation"> |
| 20 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 21 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 22 | <tr> |
| 23 | <td class='online-navigation'><a rel="prev" title="4.1.1 Interaction with dynamic" |
| 24 | href="dynamic-features.html"><img src='../icons/previous.png' |
| 25 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 26 | <td class='online-navigation'><a rel="parent" title="4. Execution model" |
| 27 | href="execmodel.html"><img src='../icons/up.png' |
| 28 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 29 | <td class='online-navigation'><a rel="next" title="5. Expressions" |
| 30 | href="expressions.html"><img src='../icons/next.png' |
| 31 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 32 | <td align="center" width="100%">Python Reference Manual</td> |
| 33 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 34 | href="contents.html"><img src='../icons/contents.png' |
| 35 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 36 | <td class='online-navigation'><img src='../icons/blank.png' |
| 37 | border='0' height='32' alt='' width='32' /></td> |
| 38 | <td class='online-navigation'><a rel="index" title="Index" |
| 39 | href="genindex.html"><img src='../icons/index.png' |
| 40 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 41 | </tr></table> |
| 42 | <div class='online-navigation'> |
| 43 | <b class="navlabel">Previous:</b> |
| 44 | <a class="sectref" rel="prev" href="dynamic-features.html">4.1.1 Interaction with dynamic</A> |
| 45 | <b class="navlabel">Up:</b> |
| 46 | <a class="sectref" rel="parent" href="execmodel.html">4. Execution model</A> |
| 47 | <b class="navlabel">Next:</b> |
| 48 | <a class="sectref" rel="next" href="expressions.html">5. Expressions</A> |
| 49 | </div> |
| 50 | <hr /></div> |
| 51 | </DIV> |
| 52 | <!--End of Navigation Panel--> |
| 53 | |
| 54 | <H1><A NAME="SECTION006200000000000000000"></A><A NAME="exceptions"></A> |
| 55 | <a id='l2h-321' xml:id='l2h-321'></a> |
| 56 | <BR> |
| 57 | 4.2 Exceptions |
| 58 | </H1> |
| 59 | |
| 60 | <P> |
| 61 | Exceptions are a means of breaking out of the normal flow of control |
| 62 | of a code block in order to handle errors or other exceptional |
| 63 | conditions. An exception is |
| 64 | <em>raised</em><a id='l2h-324' xml:id='l2h-324'></a> at the point where the error |
| 65 | is detected; it may be <em>handled</em><a id='l2h-325' xml:id='l2h-325'></a> by |
| 66 | the surrounding code block or by any code block that directly or |
| 67 | indirectly invoked the code block where the error occurred. |
| 68 | <a id='l2h-326' xml:id='l2h-326'></a> |
| 69 | |
| 70 | <P> |
| 71 | The Python interpreter raises an exception when it detects a run-time |
| 72 | error (such as division by zero). A Python program can also |
| 73 | explicitly raise an exception with the <tt class="keyword">raise</tt> statement. |
| 74 | Exception handlers are specified with the <tt class="keyword">try</tt> ... <tt class="keyword">except</tt> |
| 75 | statement. The <tt class="keyword">try</tt> ... <tt class="keyword">finally</tt> statement |
| 76 | specifies cleanup code which does not handle the exception, but is |
| 77 | executed whether an exception occurred or not in the preceding code. |
| 78 | |
| 79 | <P> |
| 80 | Python uses the ``termination''<a id='l2h-327' xml:id='l2h-327'></a> model of |
| 81 | error handling: an exception handler can find out what happened and |
| 82 | continue execution at an outer level, but it cannot repair the cause |
| 83 | of the error and retry the failing operation (except by re-entering |
| 84 | the offending piece of code from the top). |
| 85 | |
| 86 | <P> |
| 87 | When an exception is not handled at all, the interpreter terminates |
| 88 | execution of the program, or returns to its interactive main loop. In |
| 89 | either case, it prints a stack backtrace, except when the exception is |
| 90 | <tt class="exception">SystemExit</tt><a id='l2h-323' xml:id='l2h-323'></a>. |
| 91 | |
| 92 | <P> |
| 93 | Exceptions are identified by class instances. |
| 94 | Selection of a matching except clause is based on object identity. |
| 95 | The <tt class="keyword">except</tt> clause must reference the same class or a base |
| 96 | class of it. |
| 97 | |
| 98 | <P> |
| 99 | When an exception is raised, an object (maybe <code>None</code>) is passed |
| 100 | as the exception's <em>value</em>; this object does not affect the |
| 101 | selection of an exception handler, but is passed to the selected |
| 102 | exception handler as additional information. For class exceptions, |
| 103 | this object must be an instance of the exception class being raised. |
| 104 | |
| 105 | <P> |
| 106 | <div class="warning"><b class="label">Warning:</b> |
| 107 | |
| 108 | Messages to exceptions are not part of the Python API. Their contents may |
| 109 | change from one version of Python to the next without warning and should not |
| 110 | be relied on by code which will run under multiple versions of the |
| 111 | interpreter. |
| 112 | </div> |
| 113 | |
| 114 | <P> |
| 115 | See also the description of the <tt class="keyword">try</tt> statement in |
| 116 | section <A href="try.html#try">7.4</A> and <tt class="keyword">raise</tt> statement in |
| 117 | section <A href="raise.html#raise">6.9</A>. |
| 118 | |
| 119 | <DIV CLASS="navigation"> |
| 120 | <div class='online-navigation'> |
| 121 | <p></p><hr /> |
| 122 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 123 | <tr> |
| 124 | <td class='online-navigation'><a rel="prev" title="4.1.1 Interaction with dynamic" |
| 125 | href="dynamic-features.html"><img src='../icons/previous.png' |
| 126 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 127 | <td class='online-navigation'><a rel="parent" title="4. Execution model" |
| 128 | href="execmodel.html"><img src='../icons/up.png' |
| 129 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 130 | <td class='online-navigation'><a rel="next" title="5. Expressions" |
| 131 | href="expressions.html"><img src='../icons/next.png' |
| 132 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 133 | <td align="center" width="100%">Python Reference Manual</td> |
| 134 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 135 | href="contents.html"><img src='../icons/contents.png' |
| 136 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 137 | <td class='online-navigation'><img src='../icons/blank.png' |
| 138 | border='0' height='32' alt='' width='32' /></td> |
| 139 | <td class='online-navigation'><a rel="index" title="Index" |
| 140 | href="genindex.html"><img src='../icons/index.png' |
| 141 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 142 | </tr></table> |
| 143 | <div class='online-navigation'> |
| 144 | <b class="navlabel">Previous:</b> |
| 145 | <a class="sectref" rel="prev" href="dynamic-features.html">4.1.1 Interaction with dynamic</A> |
| 146 | <b class="navlabel">Up:</b> |
| 147 | <a class="sectref" rel="parent" href="execmodel.html">4. Execution model</A> |
| 148 | <b class="navlabel">Next:</b> |
| 149 | <a class="sectref" rel="next" href="expressions.html">5. Expressions</A> |
| 150 | </div> |
| 151 | </div> |
| 152 | <hr /> |
| 153 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 154 | </DIV> |
| 155 | <!--End of Navigation Panel--> |
| 156 | <ADDRESS> |
| 157 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 158 | </ADDRESS> |
| 159 | </BODY> |
| 160 | </HTML> |