Bug #70938 [Com]: Handled Exception throws in finally block
Edit report at https://bugs.php.net/bug.php?id=70938&edit=1
ID: 70938
Comment by: nhojohl at gmail dot com
Reported by: xela at xela dot org dot ua
Summary: Handled Exception throws in finally block
Status: Open
Type: Bug
Package: Scripting Engine problem
Operating System: All
PHP Version: 5.6.15
Block user comment: N
Private report: N
New Comment:
This looks right to me.
Your first exception is caught and then outputted in the catch statement, "Server made a boo
boo".
Finally is executed regardless of whether or not an exception is throw and/or caught. It always
happens.
"Code within the finally block will always be executed after the try and catch blocks,
regardless of whether an exception has been thrown, and before normal execution resumes." (ref:
http://php.net/manual/en/language.exceptions.php)
You then attempt to create the DbTable object in the finally statement (which is executed regardless
of the previous outcome). It then proceeds to throw the new exception from the finally block
"Database made a boo boo". Thrown exceptions in a finally block are not captured by the
previous try/catch. They would require an entirely separate try/catch statement inside the finally
block itself.
Previous Comments:
------------------------------------------------------------------------
[2015-11-18 17:11:44] xela at xela dot org dot ua
Description:
------------
I think, we can have two problems (bugs?) here:
1) Handled Exception throws in finally block;
2) finally is executed even with throw in catch block.
Test script:
---------------
<?php
class DbTable {
public function __construct() {
try {
throw new \Exception('Database made a boo boo');
}
catch (\Exception $e) {
// ok, reconnect
}
}
}
try {
throw new \Exception('Server made a boo boo');
}
catch(\Exception $e) {
// do smth
echo $e->getMessage()."\n";
throw $e;
}
finally {
$db = new DbTable();
echo "Done\n";
}
Expected result:
----------------
Server made a boo boo
PHP Fatal error: Uncaught exception 'Exception' with message 'Server made a boo
boo' in /var/www/html2/sstnet-xl/public/exception-test.php:15
Stack trace:
#0 {main}
thrown in /var/www/html2/sstnet-xl/public/exception-test.php on line 15
Fatal error: Uncaught exception 'Exception' with message 'Server made a boo boo'
in /var/www/html2/sstnet-xl/public/exception-test.php on line 15
Call Stack:
0.0001 231296 1. {main}() /var/www/html2/sstnet-xl/public/exception-test.php:0
0.0001 237392 2. DbTable->__construct()
/var/www/html2/sstnet-xl/public/exception-test.php:23
Actual result:
--------------
Server made a boo boo
Done
PHP Fatal error: Uncaught exception 'Exception' with message 'Server made a boo
boo' in /var/www/html2/sstnet-xl/public/exception-test.php:15
Stack trace:
#0 {main}
thrown in /var/www/html2/sstnet-xl/public/exception-test.php on line 15
Fatal error: Uncaught exception 'Exception' with message 'Server made a boo boo'
in /var/www/html2/sstnet-xl/public/exception-test.php on line 15
Exception: Database made a boo boo in /var/www/html2/sstnet-xl/public/exception-test.php on line 6
Call Stack:
0.0001 231296 1. {main}() /var/www/html2/sstnet-xl/public/exception-test.php:0
0.0001 237392 2. DbTable->__construct()
/var/www/html2/sstnet-xl/public/exception-test.php:23
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=70938&edit=1
Thread (3 messages)