<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/language.control-structures.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'control-structures.throw.php',
    1 => 'throw',
    2 => 'throw',
  ),
  'up' => 
  array (
    0 => 'language.control-structures.php',
    1 => 'Control Structures',
  ),
  'prev' => 
  array (
    0 => 'control-structures.goto.php',
    1 => 'goto',
  ),
  'next' => 
  array (
    0 => 'control-structures.try-catch.php',
    1 => 'Try...Catch',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'language/control-structures/throw.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="control-structures.throw" class="sect1">
 <h2 class="title">throw</h2>
 <p class="verinfo">(No version information available, might only be in Git)</p>
 <p class="simpara">
  The <a href="control-structures.throw.php" class="link"><code class="literal">throw</code></a> keyword is an expression and may be used in any expression context.
  It takes an instance of a <span class="exceptionname"><a href="class.throwable.php" class="exceptionname">Throwable</a></span> object as an
  argument.
 </p>
 <div class="caution"><strong class="caution">Caution</strong>
  <p class="simpara">
   Prior to PHP 8.0, the <a href="control-structures.throw.php" class="link"><code class="literal">throw</code></a> keyword was a statement and not an expression.
  </p>
 </div>
 <p class="para">
  Attempting to <a href="control-structures.throw.php" class="link"><code class="literal">throw</code></a> a non-<span class="exceptionname"><a href="class.throwable.php" class="exceptionname">Throwable</a></span> object will
  throw an <span class="exceptionname"><a href="class.error.php" class="exceptionname">Error</a></span>.

  <div class="example" id="example-1">
   <p><strong>Example #1 Throwing a non-<span class="exceptionname"><a href="class.throwable.php" class="exceptionname">Throwable</a></span> object</strong></p>
   <div class="example-contents">
<div class="annotation-interactive phpcode"><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php
</span><span style="color: #007700">throw new class {};</span></code></pre></div>
   </div>

   <div class="example-contents"><p>The above example will output
something similar to:</p></div>
   <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
PHP Fatal error:  Uncaught Error: Cannot throw objects that do not implement Throwable in Command line code:1
Stack trace:
#0 {main}
</pre></div>
   </div>
  </div>
 </p>

 <p class="simpara">
  After using <a href="control-structures.throw.php" class="link"><code class="literal">throw</code></a> the normal flow of execution is halted and the call stack
  is unwound until the object is caught by a <a href="control-structures.try-catch.php#control-structures.catch" class="link"><code class="literal">catch</code></a> block or the global scope
  is reached. In the latter case, the program is terminated with
  a fatal error containing the stack trace indicating where the
  <span class="exceptionname"><a href="class.throwable.php" class="exceptionname">Throwable</a></span> object has been thrown.
 </p>
 <div class="example" id="example-2">
  <p><strong>Example #2 Throwing an Exception in the global scope</strong></p>
  <div class="example-contents">
<div class="annotation-interactive phpcode"><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php
</span><span style="color: #007700">echo </span><span style="color: #DD0000">"Before exception\n"</span><span style="color: #007700">;
throw new </span><span style="color: #0000BB">Exception</span><span style="color: #007700">(</span><span style="color: #DD0000">'Stop execution'</span><span style="color: #007700">);
echo </span><span style="color: #DD0000">"After exception\n"</span><span style="color: #007700">;</span></code></pre></div>
  </div>

  <div class="example-contents"><p>The above example will output
something similar to:</p></div>
  <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
Before exception
PHP Fatal error:  Uncaught Exception: Stop execution in Command line code:2
Stack trace:
#0 {main}
  thrown in Command line code on line 2
</pre></div>
  </div>
 </div>
 <div class="example" id="example-3">
  <p><strong>Example #3 Throwing an Exception in a function</strong></p>
  <div class="example-contents">
   <div class="annotation-interactive phpcode"><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php

</span><span style="color: #007700">function </span><span style="color: #0000BB">foo</span><span style="color: #007700">() {
    echo </span><span style="color: #DD0000">"Before exception\n"</span><span style="color: #007700">;
    throw new </span><span style="color: #0000BB">Exception</span><span style="color: #007700">(</span><span style="color: #DD0000">'Stop execution'</span><span style="color: #007700">);
    echo </span><span style="color: #DD0000">"After exception\n"</span><span style="color: #007700">;
}

</span><span style="color: #0000BB">foo</span><span style="color: #007700">();</span></code></pre></div>
  </div>

  <div class="example-contents"><p>The above example will output
something similar to:</p></div>
  <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
Before exception
PHP Fatal error:  Uncaught Exception: Stop execution in Command line code:3
Stack trace:
#0 Command line code(7): foo()
#1 {main}
  thrown in Command line code on line 3
</pre></div>
  </div>
 </div>

 <p class="simpara">
  As <a href="control-structures.throw.php" class="link"><code class="literal">throw</code></a> is an expression it can be used to branch out on the result
  of another expression if its value is inappropriate.
 </p>
 <div class="example" id="example-4">
  <p><strong>Example #4 Using throw as an expression</strong></p>
  <div class="example-contents"><p>Only permitted as of PHP 8.0.0.</p></div>
  <div class="example-contents">
<div class="annotation-interactive phpcode"><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php

</span><span style="color: #007700">function </span><span style="color: #0000BB">test</span><span style="color: #007700">() {
    </span><span style="color: #0000BB">do_something_risky</span><span style="color: #007700">() or throw new </span><span style="color: #0000BB">Exception</span><span style="color: #007700">(</span><span style="color: #DD0000">'It did not work'</span><span style="color: #007700">);
}

function </span><span style="color: #0000BB">do_something_risky</span><span style="color: #007700">() {
    return </span><span style="color: #0000BB">false</span><span style="color: #007700">; </span><span style="color: #FF8000">// Simulate failure
</span><span style="color: #007700">}

try {
    </span><span style="color: #0000BB">test</span><span style="color: #007700">();
} catch (</span><span style="color: #0000BB">Exception $e</span><span style="color: #007700">) {
    print </span><span style="color: #0000BB">$e</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getMessage</span><span style="color: #007700">();
}</span></code></pre></div>
  </div>

  <div class="example-contents"><p>The above example will output:</p></div>
  <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
It did not work
</pre></div>
  </div>
 </div>

 <p class="simpara">
  It is possible to handle a thrown <span class="exceptionname"><a href="class.throwable.php" class="exceptionname">Throwable</a></span> by using
  a <a href="control-structures.try-catch.php" class="link">try-catch</a> block.
  See the <a href="language.error-handling.throwable.php" class="link">error handling</a>
  section for more information.
 </p>
</div><?php manual_footer($setup); ?>