Bug #68270 [Com]: Exception from finally block has exception from try block magically injected

From: Date: Mon, 20 Oct 2014 17:00:12 +0000
Subject: Bug #68270 [Com]: Exception from finally block has exception from try block magically injected
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-188207@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=68270&edit=1 ID: 68270 Comment by: Danack at basereality dot com Reported by: dan dot lugg at gmail dot com Summary: Exception from finally block has exception from try block magically injected Status: Open Type: Bug Package: *General Issues Operating System: Windows 7 x64 PHP Version: 5.5.18 Block user comment: N Private report: N New Comment: 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 Previous Comments: ------------------------------------------------------------------------ [2014-10-20 16:53:02] Danack at basereality dot com 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 } ------------------------------------------------------------------------ [2014-10-20 16:48:27] 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 } } ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=68270&edit=1

« previous php.bugs (#188207) next »