Req #54043 [Opn->Csd]: Remove inconsitency of internal exceptions and user defined exceptions
Edit report at https://bugs.php.net/bug.php?id=54043&edit=1
ID: 54043
Updated by: nikic@php.net
Reported by: sht dot alien at gmx dot net
Summary: Remove inconsitency of internal exceptions and user
defined exceptions
-Status: Open
+Status: Closed
Type: Feature/Change Request
Package: Unknown/Other Function
Operating System: Ubuntu Linux 10.04 x64
PHP Version: 5.3.5
Block user comment: N
Private report: N
New Comment:
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
Previous Comments:
------------------------------------------------------------------------
[2013-06-25 08:51:04] 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.
------------------------------------------------------------------------
[2011-02-18 10:10:24] 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 ;-)
------------------------------------------------------------------------
[2011-02-18 10:08:28] 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
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=54043&edit=1
Thread (4 messages)