<?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.finally.php',
    1 => 'finally',
    2 => 'finally',
  ),
  'up' => 
  array (
    0 => 'language.control-structures.php',
    1 => 'Control Structures',
  ),
  'prev' => 
  array (
    0 => 'control-structures.try-catch.php',
    1 => 'Try...Catch',
  ),
  'next' => 
  array (
    0 => 'language.functions.php',
    1 => 'Functions',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'language/control-structures/finally.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="control-structures.finally" class="sect1">
 <h2 class="title">finally</h2>
 <p class="verinfo">(No version information available, might only be in Git)</p>
 <p class="simpara">
  A <a href="control-structures.finally.php" class="link"><code class="literal">finally</code></a> block may also be specified after or instead of <a href="control-structures.try-catch.php#control-structures.catch" class="link"><code class="literal">catch</code></a> blocks.
  Code within the <a href="control-structures.finally.php" class="link"><code class="literal">finally</code></a> block will always be executed after the <a href="control-structures.try-catch.php#control-structures.try" class="link"><code class="literal">try</code></a> and
  <a href="control-structures.try-catch.php#control-structures.catch" class="link"><code class="literal">catch</code></a> blocks, even if a <a href="function.return.php" class="link"><code class="literal">return</code></a> or <a href="language.generators.syntax.php#control-structures.yield" class="link"><code class="literal">yield</code></a> statement is encountered,
  or if a <span class="exceptionname"><a href="class.throwable.php" class="exceptionname">Throwable</a></span> error is not caught or is rethrown.
  It will be executed prior to resuming the normal execution flow.
  The only exception to this rule is when the script is terminated abruptly,
  such as by a call to <span class="function"><a href="function.exit.php" class="function">exit()</a></span> or by a fatal error
  (for example when the maximum execution time is exceeded).
 </p>
 <div class="example" id="example-1">
  <p><strong>Example #1 <span class="exceptionname"><a href="class.throwable.php" class="exceptionname">Throwable</a></span> error handling with a <a href="control-structures.finally.php" class="link"><code class="literal">finally</code></a> block</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">inverse</span><span style="color: #007700">(</span><span style="color: #0000BB">$x</span><span style="color: #007700">) {
    return </span><span style="color: #0000BB">1</span><span style="color: #007700">/</span><span style="color: #0000BB">$x</span><span style="color: #007700">;
}

try {
    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">;
} catch (</span><span style="color: #0000BB">\DivisionByZeroError $e</span><span style="color: #007700">) {
    echo </span><span style="color: #DD0000">'Caught error: '</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">;
} finally {
    echo </span><span style="color: #DD0000">"First finally.\n"</span><span style="color: #007700">;
}

try {
    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">;
} catch (</span><span style="color: #0000BB">\DivisionByZeroError $e</span><span style="color: #007700">) {
    echo </span><span style="color: #DD0000">'Caught error: '</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">;
} finally {
    echo </span><span style="color: #DD0000">"Second finally.\n"</span><span style="color: #007700">;
}

</span><span style="color: #FF8000">// Continue execution
</span><span style="color: #007700">echo </span><span style="color: #DD0000">"Hello World\n"</span><span style="color: #007700">;</span></code></pre></div>
  </div>

  <div class="example-contents"><p>Output of the above example in PHP 8:</p></div>
  <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
0.2
First finally.
Caught error: Division by zero
Second finally.
Hello World
</pre></div>
  </div>
 </div>

 <p class="simpara">
  When the call stack is unwound after a <span class="exceptionname"><a href="class.throwable.php" class="exceptionname">Throwable</a></span> error
  has been thrown, all <a href="control-structures.finally.php" class="link"><code class="literal">finally</code></a> blocks it encounters along the way will be executed.
  In other words, nested <a href="control-structures.finally.php" class="link"><code class="literal">finally</code></a> blocks are executed even if the
  <span class="exceptionname"><a href="class.throwable.php" class="exceptionname">Throwable</a></span> error is not caught by an adjacent
  <a href="control-structures.try-catch.php#control-structures.catch" class="link"><code class="literal">catch</code></a> block.
 </p>
 <div class="example" id="example-2">
  <p><strong>Example #2 All <a href="control-structures.finally.php" class="link"><code class="literal">finally</code></a> block will be executed</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">try {
    try {
        throw new </span><span style="color: #0000BB">Exception</span><span style="color: #007700">();
    } finally {
        echo </span><span style="color: #DD0000">"Inner finally\n"</span><span style="color: #007700">;
    }
} catch (</span><span style="color: #0000BB">Exception $e</span><span style="color: #007700">) {
} finally {
    echo </span><span style="color: #DD0000">"Outer finally\n"</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>
Inner finally
Outer finally
</pre></div>
  </div>
 </div>

 <div class="sect2">
  <h3 class="title">Interaction between a <a href="control-structures.finally.php" 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</h3>
  <p class="simpara">
   One notable interaction is between the <a href="control-structures.finally.php" 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="control-structures.try-catch.php#control-structures.try" class="link"><code class="literal">try</code></a> or the <a href="control-structures.try-catch.php#control-structures.catch" class="link"><code class="literal">catch</code></a> blocks,
   the <a href="control-structures.finally.php" 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="control-structures.finally.php" class="link"><code class="literal">finally</code></a> block
   is executed. Additionally, if the <a href="control-structures.finally.php" 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="control-structures.finally.php" class="link"><code class="literal">finally</code></a> block is returned.
  </p>
  <div class="example" id="example-3">
   <p><strong>Example #3 Interaction between <a href="function.return.php" class="link"><code class="literal">return</code></a> in <a href="control-structures.finally.php" class="link"><code class="literal">finally</code></a> block and a previous <a href="function.return.php" class="link"><code class="literal">return</code></a></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">testReturnTry</span><span style="color: #007700">() {
    try {
        echo </span><span style="color: #DD0000">'Within try'</span><span style="color: #007700">, </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;
        return </span><span style="color: #DD0000">'try'</span><span style="color: #007700">;
    } finally {
        echo </span><span style="color: #DD0000">'Within finally'</span><span style="color: #007700">, </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;
        return </span><span style="color: #DD0000">'finally'</span><span style="color: #007700">;
    }
}

function </span><span style="color: #0000BB">testReturnCatch</span><span style="color: #007700">() {
    try {
        throw new </span><span style="color: #0000BB">Exception</span><span style="color: #007700">(</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">);
    } catch (</span><span style="color: #0000BB">Exception $e</span><span style="color: #007700">) {
        echo </span><span style="color: #DD0000">'Within catch'</span><span style="color: #007700">, </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;
        return </span><span style="color: #DD0000">'catch'</span><span style="color: #007700">;
    } finally {
        echo </span><span style="color: #DD0000">'Within finally'</span><span style="color: #007700">, </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;
        return </span><span style="color: #DD0000">'finally'</span><span style="color: #007700">;
    }
}

echo </span><span style="color: #0000BB">testReturnTry</span><span style="color: #007700">(), </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;
echo </span><span style="color: #0000BB">testReturnCatch</span><span style="color: #007700">(), </span><span style="color: #DD0000">"\n"</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>
Within try
Within finally
finally
Within catch
Within finally
finally
</pre></div>
   </div>
  </div>
  <div class="example" id="example-4">
   <p><strong>Example #4 Interaction between the <a href="control-structures.finally.php" 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="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">testReturnTry</span><span style="color: #007700">() {
    try {
        return </span><span style="color: #DD0000">'try'</span><span style="color: #007700">;
    } finally {
        echo </span><span style="color: #DD0000">'finally '</span><span style="color: #007700">;
    }
}

function </span><span style="color: #0000BB">testReturnCatch</span><span style="color: #007700">() {
    try {
        throw new </span><span style="color: #0000BB">Exception</span><span style="color: #007700">(</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">);
    } catch (</span><span style="color: #0000BB">Exception $e</span><span style="color: #007700">) {
        return </span><span style="color: #DD0000">'catch'</span><span style="color: #007700">;
    } finally {
        echo </span><span style="color: #DD0000">'finally '</span><span style="color: #007700">;
    }
}

echo </span><span style="color: #0000BB">testReturnTry</span><span style="color: #007700">(), </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;
echo </span><span style="color: #0000BB">testReturnCatch</span><span style="color: #007700">(), </span><span style="color: #DD0000">"\n"</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>
finally try
finally catch
</pre></div>
   </div>
  </div>
 </div>

 <div class="sect2">
  <h3 class="title">Interaction between a <a href="control-structures.finally.php" class="link"><code class="literal">finally</code></a> block and an exception thrown in a <a href="control-structures.try-catch.php#control-structures.try" class="link"><code class="literal">try</code></a> block</h3>
  <p class="simpara">
   If both the <a href="control-structures.try-catch.php#control-structures.try" class="link"><code class="literal">try</code></a> block and the <a href="control-structures.finally.php" class="link"><code class="literal">finally</code></a> block throw an exception,
   the exception thrown from the <a href="control-structures.finally.php" class="link"><code class="literal">finally</code></a> block is the one that is propagated,
   and the exception thrown from the <a href="control-structures.try-catch.php#control-structures.try" class="link"><code class="literal">try</code></a> block will be used as its previous exception.
  </p>
  <div class="example" id="example-5">
   <p><strong>Example #5 Exception in try and in finally</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">try {
    try {
        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">));
    } finally {
        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">));
    }
} catch (</span><span style="color: #0000BB">Exception $e</span><span style="color: #007700">) {
    </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">(),
        </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">(),
        </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">(),
        </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">(),
    );
}</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>
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 class="sect2">
  <h3 class="title">
   Behaviour of a <a href="control-structures.finally.php" class="link"><code class="literal">finally</code></a> block in a
   <a href="language.generators.php" class="link">Generator</a>
  </h3>

  <p class="simpara">
   If during the traversal of a generator a <span class="exceptionname"><a href="class.throwable.php" class="exceptionname">Throwable</a></span>
   error is thrown, the <a href="control-structures.finally.php" class="link"><code class="literal">finally</code></a> within the generator will be executed,
   as it is the previous element of the call stack.
  </p>
  <div class="example" id="example-6">
   <p><strong>Example #6 
    <a href="control-structures.finally.php" class="link"><code class="literal">finally</code></a> block being run when an <span class="exceptionname"><a href="class.exception.php" class="exceptionname">Exception</a></span>
    has been thrown
   </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">generate</span><span style="color: #007700">()
{
    try {
        yield </span><span style="color: #0000BB">1</span><span style="color: #007700">;
        yield </span><span style="color: #0000BB">2</span><span style="color: #007700">;
    } catch(</span><span style="color: #0000BB">\Exception $e</span><span style="color: #007700">) {
        echo </span><span style="color: #DD0000">'catch'</span><span style="color: #007700">;
    } finally {
        echo </span><span style="color: #DD0000">"finally\n"</span><span style="color: #007700">;
    }
}

try {
    foreach (</span><span style="color: #0000BB">generate</span><span style="color: #007700">() as </span><span style="color: #0000BB">$i</span><span style="color: #007700">) {
        echo </span><span style="color: #0000BB">$i</span><span style="color: #007700">, </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;
        throw new </span><span style="color: #0000BB">Exception</span><span style="color: #007700">();
    }
} catch(</span><span style="color: #0000BB">Exception $e</span><span style="color: #007700">) {
    echo </span><span style="color: #DD0000">'outer catch'</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>
1
finally
outer catch
</pre></div>
   </div>
  </div>

  <p class="simpara">
   It is possible to <a href="language.generators.syntax.php#control-structures.yield" class="link"><code class="literal">yield</code></a> within a <a href="control-structures.finally.php" class="link"><code class="literal">finally</code></a> block but if a
   <span class="exceptionname"><a href="class.throwable.php" class="exceptionname">Throwable</a></span> error occurs and is not caught within
   the <a href="control-structures.foreach.php" class="link"><code class="literal">foreach</code></a> a <code class="literal">Cannot yield from finally in a force-closed
   generator</code> <span class="exceptionname"><a href="class.error.php" class="exceptionname">Error</a></span> is thrown.
  </p>
  <div class="example" id="example-7">
   <p><strong>Example #7 
    Error when <a href="language.generators.syntax.php#control-structures.yield" class="link"><code class="literal">yield</code></a>ing in <a href="control-structures.finally.php" class="link"><code class="literal">finally</code></a> block when an <span class="exceptionname"><a href="class.exception.php" class="exceptionname">Exception</a></span>
    has been thrown
   </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">generate</span><span style="color: #007700">()
{
    try {
        yield </span><span style="color: #0000BB">1</span><span style="color: #007700">;
        yield </span><span style="color: #0000BB">2</span><span style="color: #007700">;
    } catch(</span><span style="color: #0000BB">\Exception $e</span><span style="color: #007700">) {
        echo </span><span style="color: #DD0000">'catch'</span><span style="color: #007700">;
    } finally {
        echo </span><span style="color: #DD0000">"finally\n"</span><span style="color: #007700">;
        yield </span><span style="color: #0000BB">3</span><span style="color: #007700">;
    }
}

try {
    foreach (</span><span style="color: #0000BB">generate</span><span style="color: #007700">() as </span><span style="color: #0000BB">$i</span><span style="color: #007700">) {
        echo </span><span style="color: #0000BB">$i</span><span style="color: #007700">, </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;
        throw new </span><span style="color: #0000BB">Exception</span><span style="color: #007700">();
    }
} catch(</span><span style="color: #0000BB">Exception $e</span><span style="color: #007700">) {
    echo </span><span style="color: #DD0000">'outer catch'</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>
1
finally

Fatal error: Uncaught Exception in /in/GbFmu:19
Stack trace:
#0 {main}

Next Error: Cannot yield from finally in a force-closed generator in /in/GbFmu:12
Stack trace:
#0 /in/GbFmu(19): generate()
#1 {main}
  thrown in /in/GbFmu on line 12
</pre></div>
   </div>
  </div>
 </div>

 <div class="sect2">
  <h3 class="title">Interaction between a <a href="control-structures.finally.php" class="link"><code class="literal">finally</code></a> block and a call to <span class="function"><a href="function.exit.php" class="function">exit()</a></span></h3>
  <p class="simpara">
   When <span class="function"><a href="function.exit.php" class="function">exit()</a></span> is called within a <a href="control-structures.try-catch.php#control-structures.try" class="link"><code class="literal">try</code></a> or a <a href="control-structures.try-catch.php#control-structures.catch" class="link"><code class="literal">catch</code></a> block
   all <a href="control-structures.finally.php" class="link"><code class="literal">finally</code></a> blocks are skipped.
  </p>
  <div class="example" id="example-8">
   <p><strong>Example #8 Interaction between <a href="control-structures.finally.php" class="link"><code class="literal">finally</code></a> block and <span class="function"><a href="function.exit.php" class="function">exit()</a></span> called in <a href="control-structures.try-catch.php#control-structures.try" class="link"><code class="literal">try</code></a> block</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">try {
    echo </span><span style="color: #DD0000">'quit'</span><span style="color: #007700">;
    exit(</span><span style="color: #0000BB">0</span><span style="color: #007700">);
} finally {
    echo </span><span style="color: #DD0000">'finally'</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>
quit
</pre></div>
   </div>
  </div>
  <div class="example" id="example-9">
   <p><strong>Example #9 Interaction between <a href="control-structures.finally.php" class="link"><code class="literal">finally</code></a> block and <span class="function"><a href="function.exit.php" class="function">exit()</a></span> called in <a href="control-structures.try-catch.php#control-structures.catch" class="link"><code class="literal">catch</code></a> block</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">try {
    throw new </span><span style="color: #0000BB">Exception</span><span style="color: #007700">(</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">);
} catch (</span><span style="color: #0000BB">Exception $e</span><span style="color: #007700">) {
    echo </span><span style="color: #DD0000">'catch'</span><span style="color: #007700">;
    exit(</span><span style="color: #0000BB">0</span><span style="color: #007700">);
} finally {
    echo </span><span style="color: #DD0000">'finally '</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>
catch
</pre></div>
   </div>
  </div>
  <div class="example" id="example-10">
   <p><strong>Example #10 All <a href="control-structures.finally.php" class="link"><code class="literal">finally</code></a> blocks are skipped when <span class="function"><a href="function.exit.php" class="function">exit()</a></span> is called</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">try {
    try {
        echo </span><span style="color: #DD0000">'quit'</span><span style="color: #007700">;
        exit(</span><span style="color: #0000BB">0</span><span style="color: #007700">);
    } finally {
        echo </span><span style="color: #DD0000">'inner finally '</span><span style="color: #007700">;
    }
} finally {
    echo </span><span style="color: #DD0000">'outer finally'</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>
quit
</pre></div>
   </div>
  </div>

  <p class="simpara">
   When <span class="function"><a href="function.exit.php" class="function">exit()</a></span> is called within a <a href="control-structures.finally.php" class="link"><code class="literal">finally</code></a> block,
   all other outer <a href="control-structures.finally.php" class="link"><code class="literal">finally</code></a> blocks are skipped.
  </p>
  <div class="example" id="example-11">
   <p><strong>Example #11 Skipping outer <a href="control-structures.finally.php" class="link"><code class="literal">finally</code></a> block by calling <span class="function"><a href="function.exit.php" class="function">exit()</a></span></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">try {
    try {
        echo </span><span style="color: #DD0000">'try'</span><span style="color: #007700">, </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;
    } finally {
        echo </span><span style="color: #DD0000">'inner finally '</span><span style="color: #007700">;
        exit(</span><span style="color: #0000BB">0</span><span style="color: #007700">);
    }
} finally {
    echo </span><span style="color: #DD0000">'outer finally'</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>
try
inner finally
</pre></div>
   </div>
  </div>

  <div class="sect3">
   <h4 class="title">Edge case of calling <span class="function"><a href="function.exit.php" class="function">exit()</a></span> in a <a href="control-structures.foreach.php" class="link"><code class="literal">foreach</code></a> over a generator</h4>
   <p class="simpara">
    This one edge case behaviour is highly dependent on which version of PHP
    the code is executed.
   </p>

   <div class="informalexample">
    <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">generate</span><span style="color: #007700">()
{
    try {
        yield </span><span style="color: #0000BB">1</span><span style="color: #007700">;
        yield </span><span style="color: #0000BB">2</span><span style="color: #007700">;
    } catch(</span><span style="color: #0000BB">\Exception $e</span><span style="color: #007700">) {
        echo </span><span style="color: #DD0000">'catch'</span><span style="color: #007700">;
    } finally {
        echo </span><span style="color: #DD0000">"inner finally\n"</span><span style="color: #007700">;
    }
}

try {
    foreach (</span><span style="color: #0000BB">generate</span><span style="color: #007700">() as </span><span style="color: #0000BB">$i</span><span style="color: #007700">) {
        echo </span><span style="color: #0000BB">$i</span><span style="color: #007700">, </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;
        exit(</span><span style="color: #0000BB">0</span><span style="color: #007700">);
    }
} finally {
    echo </span><span style="color: #DD0000">"outer finally\n"</span><span style="color: #007700">;
}</span></code></pre></div>
    </div>

    <p class="simpara">
     Output of the above example prior to PHP 7.1.14, in PHP 7.2.0 and 7.2.1,
     and as of PHP 8.0.0
    </p>
    <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
1
inner finally
</pre></div>
    </div>
    <p class="simpara">
     Output of the above example as of PHP 7.1.14 and 7.2.2,
     and prior to PHP 8.0.0
    </p>
    <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
1
</pre></div>
    </div>
   </div>
  </div>
 </div>
</div><?php manual_footer($setup); ?>