php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #54043 Remove inconsitency of internal exceptions and user defined exceptions
Submitted: 2011-02-18 10:08 UTC Modified: 2011-02-18 10:10 UTC
Votes:10
Avg. Score:4.4 ± 0.8
Reproduced:8 of 8 (100.0%)
Same Version:3 (37.5%)
Same OS:3 (37.5%)
From: sht dot alien at gmx dot net Assigned:
Status: Closed Package: Unknown/Other Function
PHP Version: 5.3.5 OS: Ubuntu Linux 10.04 x64
Private report: No CVE-ID: None
 [2011-02-18 10:08 UTC] sht dot alien at gmx dot net
Description:
------------
Exceptions thrown by standard PHP classes are handled inconsistently to user defined exceptions, but should not.

An internal exception, like the one thrown by DateTime::__construct() when supplying and invalid date string (like '9999-11-33') also trigger a E_WARNING.
User defined Exceptions do not trigger an additional E_WARNING.

If you create a custom error handler which converts all PHP errors to exceptions, catching internal exceptions inline therefore throws another Exception in the custom error handler, making it impossible to really catch the Exception.

REQUEST:
Both exception types should work consistently likewise, preferrably without triggering an E_WARNING.

Or there should be means to distinguish an error triggered by an internal exception from an actual error and provide data if the exception was already handled/catched.

Test script:
---------------
<?php
//////////////////////////////////
// PHP INTERNAL EXCEPTION:
//////////////////////////////////

register_shutdown_function('shutdown');

$time		= '9999-11-33';	// obviously invalid ;-)
$timeZone	= new DateTimeZone('UTC');

try {
	$dateTime	= new DateTime($time, $timeZone);
} catch (Exception $e) {
	var_dump('Exception:', $e->getMessage());
}

echo 'END' . PHP_EOL;


function shutdown()
{
	$error = error_get_last();
	var_dump('Error ', @$error);
}
?>


<?php

//////////////////////////////////
// USER DEFINED EXCEPTION:
//////////////////////////////////

register_shutdown_function('shutdown');

try {
	throw new Exception('Foo Exception');
} catch (Exception $e) {
	var_dump('Exception:', $e->getMessage());
}

echo 'END' . PHP_EOL;


function shutdown()
{
	$error = error_get_last();
	var_dump('Error ', @$error);
}
?>

Expected result:
----------------
Output DateTime::__construct() exception:

string(10) "Exception:"
string(105) "DateTime::__construct(): Failed to parse time string (9999-11-33) at position 9 (3): Unexpected character"
END
string(6) "Error "
array(4) {
  ["type"]=>
  int(2)
  ["message"]=>
  string(105) "DateTime::__construct(): Failed to parse time string (9999-11-33) at position 9 (3): Unexpected character"
  ["file"]=>
  string(67) "/home/jebner/Zend/workspaces/DefaultWorkspace7/sandbox/datetime.php"
  ["line"]=>
  int(8)
}


Output user defined exception:

string(10) "Exception:"
string(13) "Foo Exception"
END
string(6) "Error "
NULL


Actual result:
--------------
Output DateTime::__construct() exception:

string(10) "Exception:"
string(105) "DateTime::__construct(): Failed to parse time string (9999-11-33) at position 9 (3): Unexpected character"
END
string(4) "Error "
NULL
}


Output user defined exception:

string(10) "Exception:"
string(13) "Foo Exception"
END
string(6) "Error "
NULL


Patches

error.patch (last revision 2013-06-25 08:29 UTC by jwalton at m3hc dot com)

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-02-18 10:10 UTC] sht dot alien at gmx dot net
Sorry, I mixed up actual result and expected result. It should be the other way round...
So actual result is expected result, and expected result is actual result ;-)
 [2013-06-25 08:51 UTC] jwalton at m3hc dot com
Added patch which puts setting last error until AFTER its determined that an exception is thrown, and will only set it, if no exception is actually thrown.

I'm not really sure the purpose of setting the error if no error is actually thrown (ala not suppressed by @)

IMO, it would make MORE sense to have shutdown function know if it is being shutdown because of an error.
 [2017-12-23 12:35 UTC] nikic@php.net
Automatic comment on behalf of nikita.ppv@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=688b9136abcbfe28ca5c0fe6aae6143c4da58f73
Log: Fixed bug #54043
 [2017-12-23 12:35 UTC] nikic@php.net
-Status: Open +Status: Closed
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 11:01:29 2024 UTC