note 128041 added to language.exceptions
| From: | ilia-yats at ukr dot net | Date: | Fri, 23 Dec 2022 00:24:20 +0000 |
| Subject: | note 128041 added to language.exceptions | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-219997@lists.php.net to get a copy of this message | ||
Note some undocumented details about exceptions thrown from 'finally' blocks.
When exception is thrown from 'finally' block, it overrides the original not-caught (or
re-thrown) exception. So the behavior is similar to 'return': value returned from
'finally' overrides the one returned earlier. And the original exception is automatically
appended to the exceptions chain, i.e. becomes 'previous' for the new one. Example:
<?php
try {
try {
throw new Exception('thrown from try');
} finally {
throw new Exception('thrown from finally');
}
} catch(Exception $e) {
echo $e->getMessage();
echo PHP_EOL;
echo $e->getPrevious()->getMessage();
}
// will output:
// thrown from finally
// thrown from try
?>
Example with re-throwing:
<?php
try {
try {
throw new Exception('thrown from try');
} catch (Exception $e) {
throw new Exception('thrown from catch');
} finally {
throw new Exception('thrown from finally');
}
} catch(Exception $e) {
echo $e->getMessage();
echo PHP_EOL;
echo $e->getPrevious()->getMessage();
}
// will output:
// thrown from finally
// thrown from catch
?>
The same happens even if explicitly pass null as previous exception:
<?php
try {
try {
throw new Exception('thrown from try');
} finally {
throw new Exception('thrown from finally', null, null);
}
} catch(Exception $e) {
echo $e->getMessage();
echo PHP_EOL;
echo $e->getPrevious()->getMessage();
}
// will output:
// thrown from finally
// thrown from try
?>
Also it is possible to pass previous exception explicitly, the 'original' one will be
still appended to the chain, e.g.:
<?php
try {
try {
throw new Exception('thrown from try');
} finally {
throw new Exception(
'thrown from finally',
null,
new Exception('Explicitly set previous!')
);
}
} catch(Exception $e) {
echo $e->getMessage();
echo PHP_EOL;
echo $e->getPrevious()->getMessage();
echo PHP_EOL;
echo $e->getPrevious()->getPrevious()->getMessage();
}
// will output:
// thrown from finally
// Explicitly set previous!
// thrown from try
?>
This seems to be true for versions 5.6-8.2.
----
Server IP: 45.112.84.4
Probable Submitter: 185.85.3.54 (proxied: 188.163.15.175)
----
Manual Page -- https://php.net/manual/en/language.exceptions.php
Edit -- https://main.php.net/note/edit/128041
Del: integrated -- https://main.php.net/note/delete/128041/integrated
Del: useless -- https://main.php.net/note/delete/128041/useless
Del: bad code -- https://main.php.net/note/delete/128041/bad+code
Del: spam -- https://main.php.net/note/delete/128041/spam
Del: non-english -- https://main.php.net/note/delete/128041/non-english
Del: in docs -- https://main.php.net/note/delete/128041/in+docs
Del: other reasons-- https://main.php.net/note/delete/128041
Reject -- https://main.php.net/note/reject/128041
Search -- https://main.php.net/manage/user-notes.php