mirror of
https://github.com/sigmasternchen/php-doc-en
synced 2025-03-15 08:28:54 +00:00
Clarification on interaction between finally and return statements (doc bug #68290)
Patch by Sorin Nunca git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@349132 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
parent
738f5714c1
commit
9d7bbd34d5
1 changed files with 36 additions and 1 deletions
|
@ -263,6 +263,14 @@ class MyException extends Exception
|
|||
executed after the &try; and &catch; blocks, regardless of whether an
|
||||
exception has been thrown, and before normal execution resumes.
|
||||
</para>
|
||||
<para>
|
||||
One notable interaction is between the &finally; block and a &return; statement.
|
||||
If a &return; statement is encountered inside either the &try; or the &catch; blocks,
|
||||
the &finally; block will still be executed. Moreover, the &return; statement is
|
||||
evaluated when encountered, but the result will be returned after the &finally; block
|
||||
is executed. Additionaly, if the &finally; block also contains a &return; statement,
|
||||
the value from the &finally; block is returned.
|
||||
</para>
|
||||
</simplesect>
|
||||
|
||||
<simplesect xml:id="language.exceptions.notes">
|
||||
|
@ -323,7 +331,7 @@ Hello World
|
|||
</screen>
|
||||
</example>
|
||||
<example>
|
||||
<title>Exception handling with a <literal>finally</literal> block</title>
|
||||
<title>Exception handling with a &finally; block</title>
|
||||
<programlisting role="php">
|
||||
<![CDATA[
|
||||
<?php
|
||||
|
@ -363,6 +371,33 @@ First finally.
|
|||
Caught exception: Division by zero.
|
||||
Second finally.
|
||||
Hello World
|
||||
]]>
|
||||
</screen>
|
||||
</example>
|
||||
<example>
|
||||
<title>Interaction between the &finally; block and &return;</title>
|
||||
<programlisting role="php">
|
||||
<![CDATA[
|
||||
<?php
|
||||
|
||||
function test() {
|
||||
try {
|
||||
throw new Exception('foo');
|
||||
} catch (Exception $e) {
|
||||
return 'catch';
|
||||
} finally {
|
||||
return 'finally';
|
||||
}
|
||||
}
|
||||
|
||||
echo test();
|
||||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
&example.outputs;
|
||||
<screen>
|
||||
<![CDATA[
|
||||
finally
|
||||
]]>
|
||||
</screen>
|
||||
</example>
|
||||
|
|
Loading…
Reference in a new issue