Bug #72295 [Opn->Nab]: restore_exception_handler in finally block runs before global exception handler
| From: | requinix@php.net | Date: | Mon, 30 May 2016 22:12:40 +0000 |
| Subject: | Bug #72295 [Opn->Nab]: restore_exception_handler in finally block runs before global exception handler | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-201354@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=72295&edit=1
ID: 72295
Updated by: requinix@php.net
Reported by: james dot beninger at gmail dot com
-Summary: restore_exception_handler in finally block runs
before catch
+Summary: restore_exception_handler in finally block runs
before global exception handler
-Status: Open
+Status: Not a bug
Type: Bug
Package: *General Issues
Operating System: All
PHP Version: 7.0.7
Block user comment: N
Private report: N
New Comment:
(edited the summary for clarity)
But if that were the case then restore_exception_handler() would never happen - the script will
abort after the exception handler executes. (Because the exception handler is only to give you the
option to do something with the uncaught exception, not to act as a sort of global catch.)
finally works similarly to scope: it runs once execution leaves the try/catch block. In your example
there is no catch block so PHP begins to bubble up the exception through the call stack, and as
execution leaves the try block the finally block will execute. If there was one then it would
execute first and the finally second (and if the catch block threw its own exception, bubbling that
up would happen third).
Previous Comments:
------------------------------------------------------------------------
[2016-05-30 21:03:25] james dot beninger at gmail dot com
Description:
------------
When restore_exception_handler is called in a finally block, the *restored* exception handler
handles the exception. I would expect the exception to be fully resolved before
restore_exception_handler takes effect.
The PHP documentation states "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."
Test script:
---------------
<?php
function handler1($e) {
echo "\nhandler1";
}
function handler2($e) {
echo "\nhandler2";
}
set_exception_handler('handler1');
set_exception_handler('handler2');
try {
throw new Exception(); // 'handler1' is displayed. Expected: 'handler2'
}
finally {
restore_exception_handler();
}
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=72295&edit=1