Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception updates #238

Closed
wants to merge 8 commits into from
Closed
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 75 additions & 3 deletions language/exceptions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -188,22 +188,43 @@ echo "\n\n";
of potential exceptions. Each &try; must have at least one corresponding
&catch; or &finally; block.
</para>

<para>
If an exception is thrown and its current function scope has no &catch;
block, the exception will "bubble up" the call stack to the calling
function until it finds a matching &catch; block. All &finally; 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 &catch; block, the program will
terminate with a fatal error.
Crell marked this conversation as resolved.
Show resolved Hide resolved
</para>
<para>
The thrown object must be an instance of the
<classname>Exception</classname> class or a subclass of
<classname>Exception</classname>. Trying to throw an object that is not
will result in a PHP Fatal Error.
</para>
<para>
As of PHP 8.0.0, the &throw; keyword is an expression and may be used in any expression
context. In prior versions it was a statement and must be on its own line.
Crell marked this conversation as resolved.
Show resolved Hide resolved
</para>

</simplesect>

<simplesect xml:id="language.exceptions.catch">
<title><literal>catch</literal></title>
<para>
A &catch; block defines how to respond to a thrown exception. A &catch;
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 &catch; block a thrown exception
Crell marked this conversation as resolved.
Show resolved Hide resolved
or error encounters that matches the type of the thrown object will handle
the object.
</para>
<para>
Multiple &catch; blocks can be used to catch different classes of
exceptions. Normal execution (when no exception is thrown within the &try;
block) will continue after that last &catch; block defined in sequence.
Exceptions can be &throw;n (or re-thrown) within a &catch; block.
Exceptions can be &throw;n (or re-thrown) within a &catch; block. If not,
execution will continue after the &catch; block that was triggered.
</para>
<para>
When an exception is thrown, code following the statement will not be
Expand All @@ -213,11 +234,16 @@ echo "\n\n";
been defined with <function>set_exception_handler</function>.
</para>
<para>
In PHP 7.1 and later, a &catch; block may specify multiple exceptions
As of PHP 7.1.0, a &catch; block may specify multiple exceptions
using the pipe (<literal>|</literal>) character. This is useful for when
different exceptions from different class hierarchies are handled the
same.
</para>
<para>
As of PHP 8.0.0, the variable name for a caught exception is optional.
If not specified, the &catch; block will still execute but will not
have access to the thrown object.
</para>
</simplesect>

<simplesect xml:id="language.exceptions.finally">
Expand Down Expand Up @@ -435,6 +461,52 @@ string(11) "MyException"
]]>
</screen>
</example>
<example>
<title>Omitting the caught variable</title>
<para>Only permitted in PHP 8.0.0 and later.</para>
<programlisting role="php">
<![CDATA[
<?php

class SpecificException extends Exception {}

function test() {
cmb69 marked this conversation as resolved.
Show resolved Hide resolved
throw new SpecificException('Oopsie');
}

try {
test();
}
catch (SpecificException) {
print "A SpecificException was thrown, but we don't care about the details.";
}
?>
]]>
</programlisting>
</example>
<example>
<title>Throw as an expression</title>
<para>Only permitted in PHP 8.0.0 and later.</para>
<programlisting role="php">
<![CDATA[
<?php

class SpecificException extends Exception {}

function test() {
do_something_risky() or throw new Exception('It did not work');
}

try {
test();
}
catch (Exception $e) {
print $e->getMessage();
}
?>
]]>
</programlisting>
</example>
</simplesect>

</chapter>
Expand Down