<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/langref.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'language.exceptions.php',
    1 => 'Exceptions',
    2 => 'Exceptions',
  ),
  'up' => 
  array (
    0 => 'langref.php',
    1 => 'Language Reference',
  ),
  'prev' => 
  array (
    0 => 'language.errors.php7.php',
    1 => 'Errors in PHP 7',
  ),
  'next' => 
  array (
    0 => 'language.exceptions.extending.php',
    1 => 'Extending Exceptions',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'language/exceptions.xml',
  ),
  'history' => 
  array (
  ),
  'extra_header_links' => 
  array (
    'rel' => 'alternate',
    'href' => '/manual/en/feeds/language.exceptions.atom',
    'type' => 'application/atom+xml',
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="language.exceptions" class="chapter">
 <h1 class="title">Exceptions</h1>
<h2>Table of Contents</h2><ul class="chunklist chunklist_chapter"><li><a href="language.exceptions.extending.php">Extending Exceptions</a></li></ul>

 <p class="para">
  PHP has an exception model similar to that of other programming
  languages. An exception can be <a href="language.exceptions.php" class="link"><code class="literal">throw</code></a>n, and caught (&quot;<a href="language.exceptions.php#language.exceptions.catch" class="link"><code class="literal">catch</code></a>ed&quot;) within
  PHP. Code may be surrounded in a <a href="language.exceptions.php" class="link"><code class="literal">try</code></a> block, to facilitate the catching
  of potential exceptions. Each <a href="language.exceptions.php" class="link"><code class="literal">try</code></a> must have at least one corresponding
  <a href="language.exceptions.php#language.exceptions.catch" class="link"><code class="literal">catch</code></a> or <a href="language.exceptions.php#language.exceptions.finally" class="link"><code class="literal">finally</code></a> block.
 </p>
 <p class="para">
  If an exception is thrown and its current function scope has no <a href="language.exceptions.php#language.exceptions.catch" class="link"><code class="literal">catch</code></a>
  block, the exception will &quot;bubble up&quot; the call stack to the calling
  function until it finds a matching <a href="language.exceptions.php#language.exceptions.catch" class="link"><code class="literal">catch</code></a> block. All <a href="language.exceptions.php#language.exceptions.finally" class="link"><code class="literal">finally</code></a> blocks it encounters
  along the way will be executed. If the call stack is unwound all the way to the
  global scope without encountering a matching <a href="language.exceptions.php#language.exceptions.catch" class="link"><code class="literal">catch</code></a> block, the program will
  terminate with a fatal error unless a global exception handler has been set.
 </p>
 <p class="para">
  The thrown object must be an <a href="language.operators.type.php" class="link"><code class="literal">instanceof</code></a> <span class="interfacename"><a href="class.throwable.php" class="interfacename">Throwable</a></span>.
  Trying to throw an object that is not will result in a PHP Fatal Error.
 </p>
 <p class="para">
  As of PHP 8.0.0, the <a href="language.exceptions.php" class="link"><code class="literal">throw</code></a> keyword is an expression and may be used in any expression
  context. In prior versions it was a statement and was required to be on its own line.
 </p>

  <div id="language.exceptions.catch" class="sect1">
   <h2 class="title"><code class="literal">catch</code></h2>
   <p class="para">
    A <a href="language.exceptions.php#language.exceptions.catch" class="link"><code class="literal">catch</code></a> block defines how to respond to a thrown exception. A <a href="language.exceptions.php#language.exceptions.catch" class="link"><code class="literal">catch</code></a>
    block defines one or more types of exception or error it can handle, and
    optionally a variable to which to assign the exception. (The variable was
    required prior to PHP 8.0.0.)  The first <a href="language.exceptions.php#language.exceptions.catch" class="link"><code class="literal">catch</code></a> block a thrown exception
    or error encounters that matches the type of the thrown object will handle
    the object.
   </p>
   <p class="para">
    Multiple <a href="language.exceptions.php#language.exceptions.catch" class="link"><code class="literal">catch</code></a> blocks can be used to catch different classes of
    exceptions. Normal execution (when no exception is thrown within the <a href="language.exceptions.php" class="link"><code class="literal">try</code></a>
    block) will continue after that last <a href="language.exceptions.php#language.exceptions.catch" class="link"><code class="literal">catch</code></a> block defined in sequence.
    Exceptions can be <a href="language.exceptions.php" class="link"><code class="literal">throw</code></a>n (or re-thrown) within a <a href="language.exceptions.php#language.exceptions.catch" class="link"><code class="literal">catch</code></a> block. If not,
    execution will continue after the <a href="language.exceptions.php#language.exceptions.catch" class="link"><code class="literal">catch</code></a> block that was triggered.
   </p>
   <p class="para">
    When an exception is thrown, code following the statement will not be
    executed, and PHP will attempt to find the first matching <a href="language.exceptions.php#language.exceptions.catch" class="link"><code class="literal">catch</code></a> block.
    If an exception is not caught, a PHP Fatal Error will be issued with an
    &quot;<code class="literal">Uncaught Exception ...</code>&quot; message, unless a handler has
    been defined with <span class="function"><a href="function.set-exception-handler.php" class="function">set_exception_handler()</a></span>.
   </p>
   <p class="para">
    As of PHP 7.1.0, a <a href="language.exceptions.php#language.exceptions.catch" class="link"><code class="literal">catch</code></a> block may specify multiple exceptions
    using the pipe (<code class="literal">|</code>) character. This is useful for when
    different exceptions from different class hierarchies are handled the
    same.
   </p>
   <p class="para">
    As of PHP 8.0.0, the variable name for a caught exception is optional.
    If not specified, the <a href="language.exceptions.php#language.exceptions.catch" class="link"><code class="literal">catch</code></a> block will still execute but will not
    have access to the thrown object.
   </p>
  </div>

  <div id="language.exceptions.finally" class="sect1">
   <h2 class="title"><code class="literal">finally</code></h2>
   <p class="para">
    A <a href="language.exceptions.php#language.exceptions.finally" class="link"><code class="literal">finally</code></a> block may also be specified after or
    instead of <a href="language.exceptions.php#language.exceptions.catch" class="link"><code class="literal">catch</code></a> blocks. Code within the <a href="language.exceptions.php#language.exceptions.finally" class="link"><code class="literal">finally</code></a> block will always be
    executed after the <a href="language.exceptions.php" class="link"><code class="literal">try</code></a> and <a href="language.exceptions.php#language.exceptions.catch" class="link"><code class="literal">catch</code></a> blocks, regardless of whether an
    exception has been thrown, and before normal execution resumes.
   </p>
   <p class="para">
    One notable interaction is between the <a href="language.exceptions.php#language.exceptions.finally" class="link"><code class="literal">finally</code></a> block and a <a href="function.return.php" class="link"><code class="literal">return</code></a> statement.
    If a <a href="function.return.php" class="link"><code class="literal">return</code></a> statement is encountered inside either the <a href="language.exceptions.php" class="link"><code class="literal">try</code></a> or the <a href="language.exceptions.php#language.exceptions.catch" class="link"><code class="literal">catch</code></a> blocks,
    the <a href="language.exceptions.php#language.exceptions.finally" class="link"><code class="literal">finally</code></a> block will still be executed. Moreover, the <a href="function.return.php" class="link"><code class="literal">return</code></a> statement is
    evaluated when encountered, but the result will be returned after the <a href="language.exceptions.php#language.exceptions.finally" class="link"><code class="literal">finally</code></a> block
    is executed. Additionally, if the <a href="language.exceptions.php#language.exceptions.finally" class="link"><code class="literal">finally</code></a> block also contains a <a href="function.return.php" class="link"><code class="literal">return</code></a> statement,
    the value from the <a href="language.exceptions.php#language.exceptions.finally" class="link"><code class="literal">finally</code></a> block is returned.
   </p>
   <p class="para">
    Another notable interaction is between an exception thrown from within a <a href="language.exceptions.php" class="link"><code class="literal">try</code></a> block,
    and an exception thrown from within a <a href="language.exceptions.php#language.exceptions.finally" class="link"><code class="literal">finally</code></a> block. If both blocks throw an exception,
    then the exception thrown from the <a href="language.exceptions.php#language.exceptions.finally" class="link"><code class="literal">finally</code></a> block will be the one that is propagated,
    and the exception thrown from the <a href="language.exceptions.php" class="link"><code class="literal">try</code></a> block will be used as its previous exception.
   </p>
  </div>

 <div id="language.exceptions.exception-handler" class="sect1">
  <h2 class="title">Global exception handler</h2>
  <p class="para">
   If an exception is allowed to bubble up to the global scope, it may be caught
   by a global exception handler if set. The <span class="function"><a href="function.set-exception-handler.php" class="function">set_exception_handler()</a></span>
   function can set a function that will be called in place of a <a href="language.exceptions.php#language.exceptions.catch" class="link"><code class="literal">catch</code></a> block if no
   other block is invoked. The effect is essentially the same as if the entire program
   were wrapped in a <a href="language.exceptions.php" class="link"><code class="literal">try</code></a>-<a href="language.exceptions.php#language.exceptions.catch" class="link"><code class="literal">catch</code></a> block with that function as the <a href="language.exceptions.php#language.exceptions.catch" class="link"><code class="literal">catch</code></a>.
  </p>
 </div>

 <div id="language.exceptions.notes" class="sect1">
   <h2 class="title">Notes</h2>

   <blockquote class="note"><p><strong class="note">Note</strong>: 
    <p class="para">
     Internal PHP functions mainly use
     <a href="errorfunc.configuration.php#ini.error-reporting" class="link">Error reporting</a>, only modern
     <a href="language.oop5.php" class="link">Object-oriented</a>
     extensions use exceptions. However, errors can be easily translated to
     exceptions with <a href="class.errorexception.php" class="link">ErrorException</a>.
     This technique only works with non-fatal errors, however.
    </p>
    <div class="example" id="example-1">
     <p><strong>Example #1 Converting error reporting to exceptions</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">exceptions_error_handler</span><span style="color: #007700">(</span><span style="color: #0000BB">$severity</span><span style="color: #007700">, </span><span style="color: #0000BB">$message</span><span style="color: #007700">, </span><span style="color: #0000BB">$filename</span><span style="color: #007700">, </span><span style="color: #0000BB">$lineno</span><span style="color: #007700">) {<br />    throw new </span><span style="color: #0000BB">ErrorException</span><span style="color: #007700">(</span><span style="color: #0000BB">$message</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">$severity</span><span style="color: #007700">, </span><span style="color: #0000BB">$filename</span><span style="color: #007700">, </span><span style="color: #0000BB">$lineno</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">set_error_handler</span><span style="color: #007700">(</span><span style="color: #DD0000">'exceptions_error_handler'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
     </div>

    </div>
   </p></blockquote>
   <div class="tip"><strong class="tip">Tip</strong>
    <p class="para">
     The <a href="book.spl.php#intro.spl" class="link">Standard PHP Library (SPL)</a> provides
     a good number of <a href="spl.exceptions.php" class="link">built-in
     exceptions</a>.
    </p>
   </div>
 </div>

  <div id="language.exceptions.examples" class="sect1">
   <h2 class="title">Examples</h2>

   <div class="example" id="example-2">
    <p><strong>Example #2 Throwing an Exception</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">inverse</span><span style="color: #007700">(</span><span style="color: #0000BB">$x</span><span style="color: #007700">) {<br />    if (!</span><span style="color: #0000BB">$x</span><span style="color: #007700">) {<br />        throw new </span><span style="color: #0000BB">Exception</span><span style="color: #007700">(</span><span style="color: #DD0000">'Division by zero.'</span><span style="color: #007700">);<br />    }<br />    return </span><span style="color: #0000BB">1</span><span style="color: #007700">/</span><span style="color: #0000BB">$x</span><span style="color: #007700">;<br />}<br /><br />try {<br />    echo </span><span style="color: #0000BB">inverse</span><span style="color: #007700">(</span><span style="color: #0000BB">5</span><span style="color: #007700">) . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />    echo </span><span style="color: #0000BB">inverse</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">) . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />} catch (</span><span style="color: #0000BB">Exception $e</span><span style="color: #007700">) {<br />    echo </span><span style="color: #DD0000">'Caught exception: '</span><span style="color: #007700">,  </span><span style="color: #0000BB">$e</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getMessage</span><span style="color: #007700">(), </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">// Continue execution<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"Hello World\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>The above example will output:</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
0.2
Caught exception: Division by zero.
Hello World
</pre></div>
    </div>
   </div>
   <div class="example" id="example-3">
    <p><strong>Example #3 Exception handling with a <a href="language.exceptions.php#language.exceptions.finally" class="link"><code class="literal">finally</code></a> block</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">inverse</span><span style="color: #007700">(</span><span style="color: #0000BB">$x</span><span style="color: #007700">) {<br />    if (!</span><span style="color: #0000BB">$x</span><span style="color: #007700">) {<br />        throw new </span><span style="color: #0000BB">Exception</span><span style="color: #007700">(</span><span style="color: #DD0000">'Division by zero.'</span><span style="color: #007700">);<br />    }<br />    return </span><span style="color: #0000BB">1</span><span style="color: #007700">/</span><span style="color: #0000BB">$x</span><span style="color: #007700">;<br />}<br /><br />try {<br />    echo </span><span style="color: #0000BB">inverse</span><span style="color: #007700">(</span><span style="color: #0000BB">5</span><span style="color: #007700">) . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />} catch (</span><span style="color: #0000BB">Exception $e</span><span style="color: #007700">) {<br />    echo </span><span style="color: #DD0000">'Caught exception: '</span><span style="color: #007700">,  </span><span style="color: #0000BB">$e</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getMessage</span><span style="color: #007700">(), </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />} finally {<br />    echo </span><span style="color: #DD0000">"First finally.\n"</span><span style="color: #007700">;<br />}<br /><br />try {<br />    echo </span><span style="color: #0000BB">inverse</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">) . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />} catch (</span><span style="color: #0000BB">Exception $e</span><span style="color: #007700">) {<br />    echo </span><span style="color: #DD0000">'Caught exception: '</span><span style="color: #007700">,  </span><span style="color: #0000BB">$e</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getMessage</span><span style="color: #007700">(), </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />} finally {<br />    echo </span><span style="color: #DD0000">"Second finally.\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">// Continue execution<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"Hello World\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>The above example will output:</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
0.2
First finally.
Caught exception: Division by zero.
Second finally.
Hello World
</pre></div>
    </div>
   </div>
   <div class="example" id="example-4">
    <p><strong>Example #4 Interaction between the <a href="language.exceptions.php#language.exceptions.finally" class="link"><code class="literal">finally</code></a> block and <a href="function.return.php" class="link"><code class="literal">return</code></a></strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">function </span><span style="color: #0000BB">test</span><span style="color: #007700">() {<br />    try {<br />        throw new </span><span style="color: #0000BB">Exception</span><span style="color: #007700">(</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">);<br />    } catch (</span><span style="color: #0000BB">Exception $e</span><span style="color: #007700">) {<br />        return </span><span style="color: #DD0000">'catch'</span><span style="color: #007700">;<br />    } finally {<br />        return </span><span style="color: #DD0000">'finally'</span><span style="color: #007700">;<br />    }<br />}<br /><br />echo </span><span style="color: #0000BB">test</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>The above example will output:</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
finally
</pre></div>
    </div>
   </div>
   <div class="example" id="example-5">
    <p><strong>Example #5 Nested Exception</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">class </span><span style="color: #0000BB">MyException </span><span style="color: #007700">extends </span><span style="color: #0000BB">Exception </span><span style="color: #007700">{ }<br /><br />class </span><span style="color: #0000BB">Test </span><span style="color: #007700">{<br />    public function </span><span style="color: #0000BB">testing</span><span style="color: #007700">() {<br />        try {<br />            try {<br />                throw new </span><span style="color: #0000BB">MyException</span><span style="color: #007700">(</span><span style="color: #DD0000">'foo!'</span><span style="color: #007700">);<br />            } catch (</span><span style="color: #0000BB">MyException $e</span><span style="color: #007700">) {<br />                </span><span style="color: #FF8000">// rethrow it<br />                </span><span style="color: #007700">throw </span><span style="color: #0000BB">$e</span><span style="color: #007700">;<br />            }<br />        } catch (</span><span style="color: #0000BB">Exception $e</span><span style="color: #007700">) {<br />            </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$e</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getMessage</span><span style="color: #007700">());<br />        }<br />    }<br />}<br /><br /></span><span style="color: #0000BB">$foo </span><span style="color: #007700">= new </span><span style="color: #0000BB">Test</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$foo</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">testing</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>The above example will output:</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
string(4) &quot;foo!&quot;
</pre></div>
    </div>
   </div>
   <div class="example" id="example-6">
    <p><strong>Example #6 Multi catch exception handling</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">class </span><span style="color: #0000BB">MyException </span><span style="color: #007700">extends </span><span style="color: #0000BB">Exception </span><span style="color: #007700">{ }<br /><br />class </span><span style="color: #0000BB">MyOtherException </span><span style="color: #007700">extends </span><span style="color: #0000BB">Exception </span><span style="color: #007700">{ }<br /><br />class </span><span style="color: #0000BB">Test </span><span style="color: #007700">{<br />    public function </span><span style="color: #0000BB">testing</span><span style="color: #007700">() {<br />        try {<br />            throw new </span><span style="color: #0000BB">MyException</span><span style="color: #007700">();<br />        } catch (</span><span style="color: #0000BB">MyException </span><span style="color: #007700">| </span><span style="color: #0000BB">MyOtherException $e</span><span style="color: #007700">) {<br />            </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">get_class</span><span style="color: #007700">(</span><span style="color: #0000BB">$e</span><span style="color: #007700">));<br />        }<br />    }<br />}<br /><br /></span><span style="color: #0000BB">$foo </span><span style="color: #007700">= new </span><span style="color: #0000BB">Test</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$foo</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">testing</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>The above example will output:</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
string(11) &quot;MyException&quot;
</pre></div>
    </div>
   </div>
   <div class="example" id="example-7">
    <p><strong>Example #7 Omitting the caught variable</strong></p>
    <div class="example-contents"><p>Only permitted in PHP 8.0.0 and later.</p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">class </span><span style="color: #0000BB">SpecificException </span><span style="color: #007700">extends </span><span style="color: #0000BB">Exception </span><span style="color: #007700">{}<br /><br />function </span><span style="color: #0000BB">test</span><span style="color: #007700">() {<br />    throw new </span><span style="color: #0000BB">SpecificException</span><span style="color: #007700">(</span><span style="color: #DD0000">'Oopsie'</span><span style="color: #007700">);<br />}<br /><br />try {<br />    </span><span style="color: #0000BB">test</span><span style="color: #007700">();<br />} catch (</span><span style="color: #0000BB">SpecificException</span><span style="color: #007700">) {<br />    print </span><span style="color: #DD0000">"A SpecificException was thrown, but we don't care about the details."</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>The above example will output:</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
A SpecificException was thrown, but we don&#039;t care about the details.
</pre></div>
    </div>
   </div>
   <div class="example" id="example-8">
    <p><strong>Example #8 Throw as an expression</strong></p>
    <div class="example-contents"><p>Only permitted in PHP 8.0.0 and later.</p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">function </span><span style="color: #0000BB">test</span><span style="color: #007700">() {<br />    </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">);<br />}<br /><br />function </span><span style="color: #0000BB">do_something_risky</span><span style="color: #007700">() {<br />    return </span><span style="color: #0000BB">false</span><span style="color: #007700">; </span><span style="color: #FF8000">// Simulate failure<br /></span><span style="color: #007700">}<br /><br />try {<br />    </span><span style="color: #0000BB">test</span><span style="color: #007700">();<br />} catch (</span><span style="color: #0000BB">Exception $e</span><span style="color: #007700">) {<br />    print </span><span style="color: #0000BB">$e</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getMessage</span><span style="color: #007700">();<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>The above example will output:</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
It did not work
</pre></div>
    </div>
   </div>
   <div class="example" id="example-9">
    <p><strong>Example #9 Exception in try and in finally</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">try {<br />    try {<br />        throw new </span><span style="color: #0000BB">Exception</span><span style="color: #007700">(</span><span style="color: #0000BB">message</span><span style="color: #007700">: </span><span style="color: #DD0000">'Third'</span><span style="color: #007700">, </span><span style="color: #0000BB">previous</span><span style="color: #007700">: new </span><span style="color: #0000BB">Exception</span><span style="color: #007700">(</span><span style="color: #DD0000">'Fourth'</span><span style="color: #007700">));<br />    } finally {<br />        throw new </span><span style="color: #0000BB">Exception</span><span style="color: #007700">(</span><span style="color: #0000BB">message</span><span style="color: #007700">: </span><span style="color: #DD0000">'First'</span><span style="color: #007700">, </span><span style="color: #0000BB">previous</span><span style="color: #007700">: new </span><span style="color: #0000BB">Exception</span><span style="color: #007700">(</span><span style="color: #DD0000">'Second'</span><span style="color: #007700">));<br />    }<br />} catch (</span><span style="color: #0000BB">Exception $e</span><span style="color: #007700">) {<br />    </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(<br />        </span><span style="color: #0000BB">$e</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getMessage</span><span style="color: #007700">(),<br />        </span><span style="color: #0000BB">$e</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getPrevious</span><span style="color: #007700">()-&gt;</span><span style="color: #0000BB">getMessage</span><span style="color: #007700">(),<br />        </span><span style="color: #0000BB">$e</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getPrevious</span><span style="color: #007700">()-&gt;</span><span style="color: #0000BB">getPrevious</span><span style="color: #007700">()-&gt;</span><span style="color: #0000BB">getMessage</span><span style="color: #007700">(),<br />        </span><span style="color: #0000BB">$e</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getPrevious</span><span style="color: #007700">()-&gt;</span><span style="color: #0000BB">getPrevious</span><span style="color: #007700">()-&gt;</span><span style="color: #0000BB">getPrevious</span><span style="color: #007700">()-&gt;</span><span style="color: #0000BB">getMessage</span><span style="color: #007700">(),<br />    );<br />}</span></span></code></div>
    </div>

    <div class="example-contents"><p>The above example will output:</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
string(5) &quot;First&quot;
string(6) &quot;Second&quot;
string(5) &quot;Third&quot;
string(6) &quot;Fourth&quot;
</pre></div>
    </div>
   </div>
  </div>

 

 </div>
<?php manual_footer($setup); ?>