| 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="break.html" /> |
| 13 | <link rel="prev" href="yield.html" /> |
| 14 | <link rel="parent" href="simple.html" /> |
| 15 | <link rel="next" href="break.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>6.9 The raise 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="6.8 The yield statement" |
| 25 | href="yield.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="6. Simple statements" |
| 28 | href="simple.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="6.10 The break statement" |
| 31 | href="break.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="yield.html">6.8 The yield statement</A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="simple.html">6. Simple statements</A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="break.html">6.10 The break statement</A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H1><A NAME="SECTION008900000000000000000"></A><A NAME="raise"></A> |
| 56 | <BR> |
| 57 | 6.9 The <tt class="keyword">raise</tt> statement |
| 58 | </H1> |
| 59 | <a id='l2h-513' xml:id='l2h-513'></a> |
| 60 | <P> |
| 61 | <dl><dd class="grammar"> |
| 62 | <div class="productions"> |
| 63 | <table> |
| 64 | <tr> |
| 65 | <td><a id='tok-raise_stmt' xml:id='tok-raise_stmt'>raise_stmt</a></td> |
| 66 | <td>::=</td> |
| 67 | <td>"raise" [<a class='grammartoken' href="Booleans.html#tok-expression">expression</a> ["," <a class='grammartoken' href="Booleans.html#tok-expression">expression</a> |
| 68 | ["," <a class='grammartoken' href="Booleans.html#tok-expression">expression</a>]]]</td></tr> |
| 69 | </table> |
| 70 | </div> |
| 71 | <a class="grammar-footer" |
| 72 | href="grammar.txt" type="text/plain" |
| 73 | >Download entire grammar as text.</a> |
| 74 | </dd></dl> |
| 75 | |
| 76 | <P> |
| 77 | If no expressions are present, <tt class="keyword">raise</tt> re-raises the last |
| 78 | exception that was active in the current scope. If no exception is |
| 79 | active in the current scope, a <tt class="exception">Queue.Empty</tt> exception is |
| 80 | raised indicating this error. |
| 81 | <a id='l2h-516' xml:id='l2h-516'></a> |
| 82 | <a id='l2h-514' xml:id='l2h-514'></a> |
| 83 | <P> |
| 84 | Otherwise, <tt class="keyword">raise</tt> evaluates the expressions to get three |
| 85 | objects, using <code>None</code> as the value of omitted expressions. The |
| 86 | first two objects are used to determine the <em>type</em> and |
| 87 | <em>value</em> of the exception. |
| 88 | |
| 89 | <P> |
| 90 | If the first object is an instance, the type of the exception is the |
| 91 | class of the instance, the instance itself is the value, and the |
| 92 | second object must be <code>None</code>. |
| 93 | |
| 94 | <P> |
| 95 | If the first object is a class, it becomes the type of the exception. |
| 96 | The second object is used to determine the exception value: If it is |
| 97 | an instance of the class, the instance becomes the exception value. |
| 98 | If the second object is a tuple, it is used as the argument list for |
| 99 | the class constructor; if it is <code>None</code>, an empty argument list is |
| 100 | used, and any other object is treated as a single argument to the |
| 101 | constructor. The instance so created by calling the constructor is |
| 102 | used as the exception value. |
| 103 | |
| 104 | <P> |
| 105 | If a third object is present and not <code>None</code>, it must be a |
| 106 | traceback<a id='l2h-515' xml:id='l2h-515'></a> object (see section <A href="types.html#traceback">3.2</A>), and |
| 107 | it is substituted instead of the current location as the place where |
| 108 | the exception occurred. If the third object is present and not a |
| 109 | traceback object or <code>None</code>, a <tt class="exception">TypeError</tt> exception is |
| 110 | raised. The three-expression form of <tt class="keyword">raise</tt> is useful to |
| 111 | re-raise an exception transparently in an except clause, but |
| 112 | <tt class="keyword">raise</tt> with no expressions should be preferred if the |
| 113 | exception to be re-raised was the most recently active exception in |
| 114 | the current scope. |
| 115 | |
| 116 | <P> |
| 117 | Additional information on exceptions can be found in |
| 118 | section <A href="exceptions.html#exceptions">4.2</A>, and information about handling exceptions is |
| 119 | in section <A href="try.html#try">7.4</A>. |
| 120 | |
| 121 | <P> |
| 122 | |
| 123 | <DIV CLASS="navigation"> |
| 124 | <div class='online-navigation'> |
| 125 | <p></p><hr /> |
| 126 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 127 | <tr> |
| 128 | <td class='online-navigation'><a rel="prev" title="6.8 The yield statement" |
| 129 | href="yield.html"><img src='../icons/previous.png' |
| 130 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 131 | <td class='online-navigation'><a rel="parent" title="6. Simple statements" |
| 132 | href="simple.html"><img src='../icons/up.png' |
| 133 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 134 | <td class='online-navigation'><a rel="next" title="6.10 The break statement" |
| 135 | href="break.html"><img src='../icons/next.png' |
| 136 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 137 | <td align="center" width="100%">Python Reference Manual</td> |
| 138 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 139 | href="contents.html"><img src='../icons/contents.png' |
| 140 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 141 | <td class='online-navigation'><img src='../icons/blank.png' |
| 142 | border='0' height='32' alt='' width='32' /></td> |
| 143 | <td class='online-navigation'><a rel="index" title="Index" |
| 144 | href="genindex.html"><img src='../icons/index.png' |
| 145 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 146 | </tr></table> |
| 147 | <div class='online-navigation'> |
| 148 | <b class="navlabel">Previous:</b> |
| 149 | <a class="sectref" rel="prev" href="yield.html">6.8 The yield statement</A> |
| 150 | <b class="navlabel">Up:</b> |
| 151 | <a class="sectref" rel="parent" href="simple.html">6. Simple statements</A> |
| 152 | <b class="navlabel">Next:</b> |
| 153 | <a class="sectref" rel="next" href="break.html">6.10 The break statement</A> |
| 154 | </div> |
| 155 | </div> |
| 156 | <hr /> |
| 157 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 158 | </DIV> |
| 159 | <!--End of Navigation Panel--> |
| 160 | <ADDRESS> |
| 161 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 162 | </ADDRESS> |
| 163 | </BODY> |
| 164 | </HTML> |