Bug #72532 [Opn]: try/catch, set_error_handler & not catching Excpetion on require(_once)

From: Date: Sat, 02 Jul 2016 08:41:23 +0000
Subject: Bug #72532 [Opn]: try/catch, set_error_handler & not catching Excpetion on require(_once)
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-201986@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=72532&edit=1 ID: 72532 User updated by: B at DAMNGOOD dot one Reported by: B at DAMNGOOD dot one -Summary: try/catch, set_error_handler & not catching Excpetion on require(_once)/include +Summary: try/catch, set_error_handler & not catching Excpetion on require(_once) Status: Open Type: Bug Package: *Directory/Filesystem functions Operating System: Linux (Ubuntu tested) PHP Version: Irrelevant Block user comment: N Private report: N New Comment: Title corrected (include does behave as expected) Previous Comments: ------------------------------------------------------------------------ [2016-07-02 08:39:26] B at DAMNGOOD dot one 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 this bug report at https://bugs.php.net/bug.php?id=72532&edit=1

« previous php.bugs (#201986) next »