|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2014-10-20 16:48 UTC] dan dot lugg at gmail dot com
Description:
------------
When an exception is thrown from a `finally` block, if an exception has been thrown from the `finally`'s corresponding `try` block, the `finally`-thrown exception magically has the `try`-thrown exception assigned as it's `$previous`.
Test script:
---------------
<?php
try {
try {
throw new Exception('Foo');
}
finally {
throw new Exception('Bar');
}
}
catch(Exception $exception) {
var_dump($exception);
}
Expected result:
----------------
Not certain, but I'd guess the `finally`-thrown exception should bubble, while the `try`-thrown exception is swallowed? Perhaps a different outcome, but what occurs is both unexpected and undocumented.
Actual result:
--------------
object(Exception)#2 (7) {
["message":protected]=>
string(3) "Bar"
["string":"Exception":private]=>
string(0) ""
["code":protected]=>
int(0)
["file":protected]=>
string(9) "/in/9DMoe"
["line":protected]=>
int(8)
["trace":"Exception":private]=>
array(0) {
}
["previous":"Exception":private]=> <-- How is this here?
object(Exception)#1 (7) {
["message":protected]=>
string(3) "Foo"
["string":"Exception":private]=>
string(0) ""
["code":protected]=>
int(0)
["file":protected]=>
string(9) "/in/9DMoe"
["line":protected]=>
int(5)
["trace":"Exception":private]=>
array(0) {
}
["previous":"Exception":private]=>
NULL
}
}
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 28 07:00:01 2025 UTC |
This happens when the 'previous' value is explicitly set to null as well. try { try { throw new Exception('Foo'); } finally { throw new Exception('Bar', 0, null); } } catch(Exception $exception) { $actualPrevious = $exception->getPrevious(); var_dump($actualPrevious); } Expected ======== NULL Actual ====== object(Exception)#1 (7) { ["message":protected]=> string(3) "Foo" ["string":"Exception":private]=> string(0) "" ["code":protected]=> int(0) ["file":protected]=> string(9) "/in/FUjgp" ["line":protected]=> int(5) ["trace":"Exception":private]=> array(0) { } ["previous":"Exception":private]=> NULL }It looks like the first construction of an Exception gets the previous, subsequent construction behaves as expected: Code ---- function foo(){ try { throw new Exception('First exception'); } finally { throw new Exception('Bar', 0, new Exception("This shouldn't have a previous.")); } } try { foo(); } catch(Exception $exception) { while ($exception = $exception->getPrevious()) { echo $exception->getMessage().PHP_EOL; } } Expected -------- This shouldn't have a previous. Actual ------ This shouldn't have a previous. First exception