php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #51463 ErrorException thrown from error_handler not catchable in exception handler
Submitted: 2010-04-02 13:29 UTC Modified: 2011-02-18 11:54 UTC
Votes:3
Avg. Score:3.7 ± 0.9
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:1 (50.0%)
From: tyra3l at gmail dot com Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 5.3.2 OS: Windows Xp Sp3, Debian Lenny
Private report: No CVE-ID: None
 [2010-04-02 13:29 UTC] tyra3l at gmail dot com
Description:
------------
It seems that there are some cases, when you can't catch Exceptions with exception_handler which was thrown from error_handler for some errors.

For example if you do this:

$class = new StdClass;
echo $class->$foo;
error_handler gets called, ErrorException was thrown, but the Exception wasn't catched with the exception_handler.

if you try 
echo $foo;
instead of
echo $class->$foo;
then the "same" error gets called with the error handler (by same error, I mean same parameters), but the Exception thrown in this case is successfuly catched by the exception handler.

Test script:
---------------
<?php

error_reporting(E_ALL);
ini_set('display_errors', 0);

function debug($s){
	echo "<pre>";
	var_dump($s);
	echo "</pre>";
}

set_error_handler(
	function ($errno, $errstr, $errfile, $errline ) {
		debug('error_handler');
		debug(array(
			'errno'		=>	$errno,
			'errstr'	=>	$errstr,
			'errfile'	=>	$errfile,
			'errline'	=>	$errline,
		));
		throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
	}
);

set_exception_handler(
	function(Exception $e){
		debug('exception_handler');
		debug($e);
	}
);

$class = new StdClass;
echo $class->$foo;

echo 'done';

Expected result:
----------------
string(13) "error_handler"

array(4) {
  ["errno"]=>
  int(8)
  ["errstr"]=>
  string(23) "Undefined variable: foo"
  ["errfile"]=>
  string(55) "C:\work\xampp_vc9\htdocs\default\bug\error_handling.php"
  ["errline"]=>
  int(46)
}

string(17) "exception_handler"

object(ErrorException)#4 (8) {
  ["message":protected]=>
  string(23) "Undefined variable: foo"
...

Actual result:
--------------
string(13) "error_handler"

array(4) {
  ["errno"]=>
  int(8)
  ["errstr"]=>
  string(23) "Undefined variable: foo"
  ["errfile"]=>
  string(55) "C:\work\xampp_vc9\htdocs\default\bug\error_handling.php"
  ["errline"]=>
  int(46)
}


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-04-02 13:33 UTC] tyra3l at gmail dot com
-Operating System: Windows +Operating System: Windows Xp Sp3, Debian Lenny
 [2010-04-02 13:33 UTC] tyra3l at gmail dot com
On Lenny I was testing with the dotdeb.org 5.3.2 deb, on windows this is the TS VC9 build
 [2010-04-02 13:35 UTC] tyra3l at gmail dot com
-Status: Open +Status: Closed
 [2010-04-02 13:35 UTC] tyra3l at gmail dot com
shit, my mistake, $foo will be empty, so $this->$foo will be generating a fatal error, but before that, generate a warning about $foo is empty. :/
 [2010-04-02 13:41 UTC] tyra3l at gmail dot com
But the exception handler should be called after the error handler and before accessing the empty property which gives the fatal error, isn't it?

I mean at first the interpreter tries to access the variable named $foo, then generates a E_NOTICE which is trapped by the error handler, which trows an Exception which never catched at all.
 [2010-04-02 14:56 UTC] derick@php.net
-Status: Closed +Status: Re-Opened
 [2010-04-02 15:19 UTC] tyra3l at gmail dot com
Another weird thing that I observed:
If I try to handle the fatal error from register shutdown function then the error_get_last() will return NULL if I throw the ErrorException from the error handler.
If I don't try to convert exceptions from errors, then the error_get_last() will return the fatal error in the shutdown function.

Or if I generate fatal error for example with 
new NonExistentClassName;
then the error_handler doesn't get called (because this is a fatal error), but the shutdown function gets the fatal error with error_get_last()

So it seems that there is some strange magic with this situation.

Tyrael
 [2010-05-05 20:15 UTC] whatrevolution at yahoo dot com
OP's test code, result:

( ! ) Parse error: syntax error, unexpected T_FUNCTION, expecting ')' in /var/www/php_bugs/exception_in_error_handler.php on line 13

PHP Version 5.2.10-2ubuntu6.4

System 	Linux 2.6.31-20-generic x86_64
Build Date 	Jan 6 2010 22:36:47
Server API 	Apache 2.0 Handler 
PHP API 	20041225
PHP Extension 	20060613
Zend Extension 	220060519
Debug Build 	no
Thread Safety 	disabled
Zend Memory Manager 	enabled 

Apache/2.2.12 (Ubuntu)
 [2010-05-05 21:24 UTC] tyra3l at gmail dot com
obviously.
closures was added with php 5.3
http://php.net/manual/en/functions.anonymous.php

Tyrael
 [2011-02-18 11:54 UTC] tyra3l at gmail dot com
-Status: Re-Opened +Status: Closed
 [2011-02-18 11:54 UTC] tyra3l at gmail dot com
I will try to reproduce the problem with a more clean test script, and create a new issue for that.

Tyrael
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 01:01:30 2024 UTC