php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #70938 Handled Exception throws in finally block
Submitted: 2015-11-18 17:11 UTC Modified: 2020-01-20 17:17 UTC
From: xela at xela dot org dot ua Assigned:
Status: Wont fix Package: Xdebug
PHP Version: 5.6.15 OS: All
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: xela at xela dot org dot ua
New email:
PHP Version: OS:

 

 [2015-11-18 17:11 UTC] 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



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-11-18 18:29 UTC] nhojohl at gmail dot com
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.
 [2015-11-18 18:59 UTC] requinix@php.net
-Status: Open +Status: Feedback
 [2015-11-18 18:59 UTC] requinix@php.net
I'm not seeing the actual behavior.
https://3v4l.org/0XfJD

As far as I know, it should go
1. Server made a boo boo (from the catch)
2. Done (from the finally)
3. Uncaught boo boo (from the re-throw in the catch)

There shouldn't be the database error as it was caught in the constructor.
 [2015-11-18 20:14 UTC] xela at xela dot org dot ua
-Status: Feedback +Status: Open -Package: Scripting Engine problem +Package: Xdebug
 [2015-11-18 20:14 UTC] xela at xela dot org dot ua
Sorry, you are fully correct. I tried to disable extensions and see actual behaviour only with Xdebug enabled. Without Xdebug everything works right, as you specified: https://3v4l.org/0XfJD

Can we address incorrect re-throw of "Database error" as Xdebug extension error?
Thank you for your help!
 [2020-01-20 17:17 UTC] derick@php.net
-Status: Open +Status: Wont fix
 [2020-01-20 17:17 UTC] derick@php.net
If this is Xdebug related, please file a bug report at https://bugs.xdebug.org
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 20 08:01:30 2024 UTC