php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #72532 try/catch, set_error_handler & not catching Excpetion on require(_once)
Submitted: 2016-07-02 08:39 UTC Modified: 2016-07-02 12:05 UTC
From: B at DAMNGOOD dot one Assigned: cmb (profile)
Status: Duplicate Package: *Directory/Filesystem functions
PHP Version: Irrelevant OS: Linux (Ubuntu tested)
Private report: No CVE-ID: None
 [2016-07-02 08:39 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-07-02 08:41 UTC] 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)
 [2016-07-02 08:41 UTC] B at DAMNGOOD dot one
Title corrected (include does behave as expected)
 [2016-07-02 12:05 UTC] cmb@php.net
-Status: Open +Status: Duplicate -Assigned To: +Assigned To: cmb
 [2016-07-02 12:05 UTC] cmb@php.net
I'm closing this as duplicate of bug #66216. It simply doesn't
make sense to have two reports for the same issue open.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Mon Apr 22 06:01:26 2019 UTC