Bug #72532 [NEW]: try/catch, set_error_handler & not catching Excpetion on require(_once)/include
| From: | B at DAMNGOOD dot one | Date: | Sat, 02 Jul 2016 08:39:34 +0000 |
| Subject: | Bug #72532 [NEW]: try/catch, set_error_handler & not catching Excpetion on require(_once)/include | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-201985@lists.php.net to get a copy of this message | ||
From: B at DAMNGOOD dot one
Operating system: Linux (Ubuntu tested)
PHP version: Irrelevant
Package: *Directory/Filesystem functions
Bug Type: Bug
Bug description:try/catch, set_error_handler & not catching Excpetion on require(_once)/include
Description:
------------
Surely this has everything to do with this ticket, which, suprisingly
has not been addressed since (in 5.5.6) and seems to be lingering on in
PHP ever since who knows when up till (7.0.4)! I would consider it to be
quite odd that so few people have voted for this ticket, so here comes
again, maybe with a bit of a clearer description and the scope of the
affected version of PHP.
The open ticket I found about this:
https://bugs.php.net/bug.php?id=66216&thanks=6
When set_error_handler is set, making a try/catch block with
require(_once) in it, will produce an uncaught exception. When you do
this with include, however, the error *is* caught as an exception.
Test script:
---------------
<?php
function errorHandler($errno, $errstr, $errfile, $errline, $errcontext)
{
throw new Exception("this was an error");
}
function exceptionHandler($e) {
echo 'exceptionHandler';
}
set_error_handler('errorHandler');
set_exception_handler('exceptionHandler');
try {
require('barf'); // there is no barf, but here we creating some
ourselves
require_once('barf');
} catch (Exception $ex) { // this never happens
echo $ex->getMessage();
}
Expected result:
----------------
this was an error
Actual result:
--------------
Code and response producing the unexpected result with require(_once):
$ php
<?php
function errorHandler($errno, $errstr, $errfile, $errline, $errcontext)
{
throw new Exception("this was an error");
}
function exceptionHandler($e) {
echo 'exceptionHandler';
}
set_error_handler('errorHandler');
set_exception_handler('exceptionHandler');
try {
require('barf');
} catch (Exception $ex) {
echo $ex->getMessage();
}
PHP Warning: Uncaught exception 'Exception' with message 'this was an
error' in -:4
Stack trace:
#0 -(15): errorHandler(2, 'require(barf): ...', '-', 15, Array)
#1 -(15): require()
#2 {main}
thrown in - on line 4
PHP Stack trace:
PHP 1. {main}() -:0
Warning: Uncaught exception 'Exception' with message 'this was an error'
in -:4
Stack trace:
#0 -(15): errorHandler(2, 'require(barf): ...', '-', 15, Array)
#1 -(15): require()
#2 {main}
thrown in - on line 4
Call Stack:
28.3487 234552 1. {main}() -:0
PHP Fatal error: main(): Failed opening required 'barf'
(include_path='.:/usr/share/php:/usr/share/pear') in - on line 15
PHP Stack trace:
PHP 1. {main}() -:0
Fatal error: main(): Failed opening required 'barf'
(include_path='.:/usr/share/php:/usr/share/pear') in - on line 15
Call Stack:
28.3487 234552 1. {main}() -:0
Code that DOES work, using include instead:
$ php
<?php
function errorHandler($errno, $errstr, $errfile, $errline, $errcontext)
{
throw new Exception("this was an error");
}
function exceptionHandler($e) {
echo 'exceptionHandler';
}
set_error_handler('errorHandler');
set_exception_handler('exceptionHandler');
try {
include('barf');
} catch (Exception $ex) {
echo $ex->getMessage();
}
// output: this was an error
--
Edit bug report at https://bugs.php.net/bug.php?id=72532&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=72532&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=72532&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=72532&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=72532&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=72532&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=72532&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=72532&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=72532&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=72532&r=support
Expected behavior: https://bugs.php.net/fix.php?id=72532&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=72532&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=72532&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=72532&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=72532&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=72532&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=72532&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=72532&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=72532&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=72532&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=72532&r=mysqlcfg