<div class="section" id="the-raise-statement">
<span id="raise"></span><h2>The <a class="reference internal" href="#raise"><tt class="xref std std-keyword docutils literal"><span class="pre">raise</span></tt></a> statement</h2>
<pre id="index-24">
<strong id="grammar-token-raise_stmt">raise_stmt</strong> ::=  &quot;raise&quot; [<a class="reference internal" href="expressions.html#grammar-token-expression"><tt class="xref docutils literal"><span class="pre">expression</span></tt></a> [&quot;from&quot; <a class="reference internal" href="expressions.html#grammar-token-expression"><tt class="xref docutils literal"><span class="pre">expression</span></tt></a>]]
</pre>
<p>If no expressions are present, <a class="reference internal" href="#raise"><tt class="xref std std-keyword docutils literal"><span class="pre">raise</span></tt></a> re-raises the last exception
that was active in the current scope.  If no exception is active in the current
scope, a <a class="reference internal" href="../library/exceptions.html#RuntimeError" title="RuntimeError"><tt class="xref py py-exc docutils literal"><span class="pre">RuntimeError</span></tt></a> exception is raised indicating that this is an
error.</p>
<p>Otherwise, <a class="reference internal" href="#raise"><tt class="xref std std-keyword docutils literal"><span class="pre">raise</span></tt></a> evaluates the first expression as the exception
object.  It must be either a subclass or an instance of <a class="reference internal" href="../library/exceptions.html#BaseException" title="BaseException"><tt class="xref py py-class docutils literal"><span class="pre">BaseException</span></tt></a>.
If it is a class, the exception instance will be obtained when needed by
instantiating the class with no arguments.</p>
<p>The <em class="dfn">type</em> of the exception is the exception instance&#8217;s class, the
<em class="dfn">value</em> is the instance itself.</p>
<p id="index-25">A traceback object is normally created automatically when an exception is raised
and attached to it as the <tt class="xref py py-attr docutils literal"><span class="pre">__traceback__</span></tt> attribute, which is writable.
You can create an exception and set your own traceback in one step using the
<tt class="xref py py-meth docutils literal"><span class="pre">with_traceback()</span></tt> exception method (which returns the same exception
instance, with its traceback set to its argument), like so:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s">&quot;foo occurred&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">with_traceback</span><span class="p">(</span><span class="n">tracebackobj</span><span class="p">)</span>
</pre></div>
</div>
<p id="index-26">The <tt class="docutils literal"><span class="pre">from</span></tt> clause is used for exception chaining: if given, the second
<em>expression</em> must be another exception class or instance, which will then be
attached to the raised exception as the <tt class="xref py py-attr docutils literal"><span class="pre">__cause__</span></tt> attribute (which is
writable).  If the raised exception is not handled, both exceptions will be
printed:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">try</span><span class="p">:</span>
<span class="gp">... </span>    <span class="nb">print</span><span class="p">(</span><span class="mi">1</span> <span class="o">/</span> <span class="mi">0</span><span class="p">)</span>
<span class="gp">... </span><span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">exc</span><span class="p">:</span>
<span class="gp">... </span>    <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s">&quot;Something bad happened&quot;</span><span class="p">)</span> <span class="kn">from</span> <span class="nn">exc</span>
<span class="gp">...</span>
<span class="gt">Traceback (most recent call last):</span>
  File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">2</span>, in <span class="n">&lt;module&gt;</span>
<span class="gr">ZeroDivisionError</span>: <span class="n">int division or modulo by zero</span>

<span class="go">The above exception was the direct cause of the following exception:</span>

<span class="gt">Traceback (most recent call last):</span>
  File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">4</span>, in <span class="n">&lt;module&gt;</span>
<span class="gr">RuntimeError</span>: <span class="n">Something bad happened</span>
</pre></div>
</div>
<p>A similar mechanism works implicitly if an exception is raised inside an
exception handler or a <a class="reference internal" href="compound_stmts.html#finally"><tt class="xref std std-keyword docutils literal"><span class="pre">finally</span></tt></a> clause: the previous exception is then
attached as the new exception&#8217;s <tt class="xref py py-attr docutils literal"><span class="pre">__context__</span></tt> attribute:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">try</span><span class="p">:</span>
<span class="gp">... </span>    <span class="nb">print</span><span class="p">(</span><span class="mi">1</span> <span class="o">/</span> <span class="mi">0</span><span class="p">)</span>
<span class="gp">... </span><span class="k">except</span><span class="p">:</span>
<span class="gp">... </span>    <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s">&quot;Something bad happened&quot;</span><span class="p">)</span>
<span class="gp">...</span>
<span class="gt">Traceback (most recent call last):</span>
  File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">2</span>, in <span class="n">&lt;module&gt;</span>
<span class="gr">ZeroDivisionError</span>: <span class="n">int division or modulo by zero</span>

<span class="go">During handling of the above exception, another exception occurred:</span>

<span class="gt">Traceback (most recent call last):</span>
  File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">4</span>, in <span class="n">&lt;module&gt;</span>
<span class="gr">RuntimeError</span>: <span class="n">Something bad happened</span>
</pre></div>
</div>
<p>Additional information on exceptions can be found in section <a class="reference internal" href="executionmodel.html#exceptions"><em>Exceptions</em></a>,
and information about handling exceptions is in section <a class="reference internal" href="compound_stmts.html#try"><em>The try statement</em></a>.</p>
</div>
