Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v9 / html / python / ref / exceptions.html
CommitLineData
920dae64
AT
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>
574.2 Exceptions
58</H1>
59
60<P>
61Exceptions are a means of breaking out of the normal flow of control
62of a code block in order to handle errors or other exceptional
63conditions. An exception is
64<em>raised</em><a id='l2h-324' xml:id='l2h-324'></a> at the point where the error
65is detected; it may be <em>handled</em><a id='l2h-325' xml:id='l2h-325'></a> by
66the surrounding code block or by any code block that directly or
67indirectly invoked the code block where the error occurred.
68<a id='l2h-326' xml:id='l2h-326'></a>
69
70<P>
71The Python interpreter raises an exception when it detects a run-time
72error (such as division by zero). A Python program can also
73explicitly raise an exception with the <tt class="keyword">raise</tt> statement.
74Exception handlers are specified with the <tt class="keyword">try</tt> ... <tt class="keyword">except</tt>
75statement. The <tt class="keyword">try</tt> ... <tt class="keyword">finally</tt> statement
76specifies cleanup code which does not handle the exception, but is
77executed whether an exception occurred or not in the preceding code.
78
79<P>
80Python uses the ``termination''<a id='l2h-327' xml:id='l2h-327'></a> model of
81error handling: an exception handler can find out what happened and
82continue execution at an outer level, but it cannot repair the cause
83of the error and retry the failing operation (except by re-entering
84the offending piece of code from the top).
85
86<P>
87When an exception is not handled at all, the interpreter terminates
88execution of the program, or returns to its interactive main loop. In
89either 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>
93Exceptions are identified by class instances.
94Selection of a matching except clause is based on object identity.
95The <tt class="keyword">except</tt> clause must reference the same class or a base
96class of it.
97
98<P>
99When an exception is raised, an object (maybe <code>None</code>) is passed
100as the exception's <em>value</em>; this object does not affect the
101selection of an exception handler, but is passed to the selected
102exception handler as additional information. For class exceptions,
103this object must be an instance of the exception class being raised.
104
105<P>
106<div class="warning"><b class="label">Warning:</b>
107
108Messages to exceptions are not part of the Python API. Their contents may
109change from one version of Python to the next without warning and should not
110be relied on by code which will run under multiple versions of the
111interpreter.
112</div>
113
114<P>
115See also the description of the <tt class="keyword">try</tt> statement in
116section&nbsp;<A href="try.html#try">7.4</A> and <tt class="keyword">raise</tt> statement in
117section&nbsp;<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>
157See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
158</ADDRESS>
159</BODY>
160</HTML>