php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #66216 Uncaught exception when using set_error_handler
Submitted: 2013-12-02 06:51 UTC Modified: 2021-02-11 08:43 UTC
Votes:3
Avg. Score:4.3 ± 0.9
Reproduced:1 of 3 (33.3%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: bastiaan at basenlily dot me Assigned: requinix (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 5.5.6 OS: Any
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
18 - 9 = ?
Subscribe to this entry?

 
 [2013-12-02 06:51 UTC] bastiaan at basenlily dot me
Description:
------------
I've used set_error_handler to create an error handler that throws an exception when a scripting error happens as described here:
http://us3.php.net/manual/en/class.errorexception.php

Even though I've got my commands in a try/catch block I'm getting an uncaught exception error.

Works fine in 5.4, breaks on 5.6

Test script:
---------------
function exception_error_handler($errno, $errstr, $errfile, $errline ) {
  throw new Exception('Hello world');
}
set_error_handler("exception_error_handler");

try {
  // try loading a file that does not have exif data:
  $results = @exif_read_data('test.txt', 0, true);
} catch (Exception $e) {
  echo $e-> getMessage; // "We never get here..."
};

Expected result:
----------------
Expect the echo to output our error instead of raising an uncaught exception error

Actual result:
--------------
Fatal error: Uncaught exception 'Exception' with message 'Hello world' in /Projects/test.php on line 8

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-12-02 08:11 UTC] laruence@php.net
there is a typo in your example:

should be: echo $e->getMessage();

after fixed that, I can not reproduce the bug you were describing
 [2013-12-03 05:15 UTC] bastiaan at basenlily dot me
Hey laruence,

Sorry for the typo, I was copying the code from my real project. Should have ran it. That said, corrected the typo and it is still behaving exactly the same:

Fatal error: Uncaught exception 'Exception' with message 'Hello world' in /Projects/web/test/bug.php on line 3

So far I've tested this on a machine running Mac OS X 10.8.6 and one running Windows (2012 server I think).
 [2015-06-23 11:10 UTC] theseer@php.net
There seem to be more problems with internally generated errors, as the following testcase demonstrates:

<?php

set_exception_handler(
    function(Exception $e){ 
        echo "-- handler --\n"; var_dump($e); exit(1);
    }
);
set_error_handler(
    function($severity, $message, $file, $line) { 
        throw new ErrorException($message, 0, $severity, $file, $line); 
    }
);

try {
    require 'non-existing-file.php';
} catch (\Exception $e) {
    echo "-- catch --\n";
    var_dump($e); exit(2);
}


Expected result:
Either a "classical" Fatal Error should occur or the warning for the missing file should be converted into an exception which in turn should be caught by catch() or at least by the global exception handler.


Actual result:
theseer@nyda ~/Desktop/Error $ php -v
PHP 5.6.10 (cli) (built: Jun 11 2015 08:13:55) 
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies

theseer@nyda ~/Desktop/Error $ php ex2.php 
PHP Warning:  Uncaught exception 'ErrorException' with message 'require(non-existing-file.php): failed to open stream: No such file or directory' in /home/theseer/Desktop/Error/ex2.php:13
Stack trace:
#0 /home/theseer/Desktop/Error/ex2.php(13): {closure}(2, 'require(non-exi...', '/home/theseer/D...', 13, Array)
#1 /home/theseer/Desktop/Error/ex2.php(13): require()
#2 {main}
  thrown in /home/theseer/Desktop/Error/ex2.php on line 13
PHP Fatal error:  main(): Failed opening required 'non-existing-file.php' (include_path='.:/usr/share/pear:/usr/share/php') in /home/theseer/Desktop/Error/ex2.php on line 13

theseer@nyda ~/Desktop/Error $ echo $?
255


The same behavior can be observed with my (relatively old) PHP 7 build:

theseer@nyda ~/Desktop/Error $ php70 -v
PHP 7.0.0-dev (cli) (built: Mar 25 2015 14:40:01) 
Copyright (c) 1997-2015 The PHP Group
Zend Engine v3.0.0-dev, Copyright (c) 1998-2015 Zend Technologies
 [2016-07-02 12:03 UTC] cmb@php.net
-Status: Open +Status: Verified -Package: Unknown/Other Function +Package: Scripting Engine problem
 [2016-07-02 12:03 UTC] cmb@php.net
I can reproduce the described behavior (as of PHP 5.3.18
respectively 5.4.8), see <https://3v4l.org/U9bu8>. Interestingly,
silencing the require statement results in no message at all, see
<https://3v4l.org/N42dN>.
 [2020-08-12 10:44 UTC] nikic@php.net
-Status: Verified +Status: Feedback
 [2020-08-12 10:44 UTC] nikic@php.net
@cmb's examples work fine as of PHP 7.4. Is there anything else left to do here?
 [2020-08-23 04:23 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
 [2020-08-23 04:31 UTC] requinix@php.net
-Status: No Feedback +Status: Closed -Assigned To: +Assigned To: requinix
 [2021-02-10 22:16 UTC] roman dot anasal at bdsu dot de
This is still broken for require_once, see https://3v4l.org/7ObLM
 [2021-02-10 22:34 UTC] nikic@php.net
@roman Seems to work fine as of PHP 8 at least?
 [2021-02-10 23:10 UTC] roman dot anasal at bdsu dot de
@nikic the expected output would be just "Hello World", as it is with https://3v4l.org/U9bu8

Instead it is:
> Fatal error: Uncaught Exception: Hello world in /in/7ObLM:4

and the process exits with status 255.

The Exception should be caught in line 11 but isn't.
 [2021-02-11 08:43 UTC] nikic@php.net
@roman: Oh right. This should be fixed with https://github.com/php/php-src/commit/d80d918547b676e78ccf096a24069a8b2e5dd96b.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 11:01:29 2024 UTC