From: mattsch at gmail dot com Operating system: PHP version: 5.3.22 Package: Date/time related Bug Type: Feature/Change Request Bug description:DateTime & DateTimeZone constructs should emit DateTimeException
Description: ------------ DateTime and DateTimeZone constructs currently emit Exception but they should really emit an exception related to their respective classes. The reason for this is the code can run in multiple environments (ui, phpunit, etc). If I want the exception to bubble up to the base exception in my application environment, I want to catch it and rethrow it with an exception that ultimately extends my app base exception. As a result, I must catch the base php Exception class and rethrow it with the wrapper exception which extends my base app exception. The problem comes when I try to run testcases on the class in the phpunit environment. Since phpunit emits exceptions, catching Exception will also catch phpunit environment exceptions which is not what I want. As a result, I must check the instance of the exception and allow phpunit exceptions to go through unchanged. If the DateTime and DateTimeZone classes did not throw Exception, then this would not be a problem. In fact, I don't think any php command should actually throw the main php Exception and should always throw an exception that is extended from Exception to avoid problems like this. Test script: --------------- <?php class DateTimeWrapper { private $dateTime; public function __construct($time = 'now', DateTimeZone $timezone = null, $format = null) { $this->setDateTime($time, $timezone, $format); } public function setDateTime($time = 'now', DateTimeZone $timezone = null, $format = null) { try { $this->dateTime = new DateTime($time, $timezone); } catch (Exception $e) { /* PHP DateTime really shouldn't throw the catch all Exception so the workaround is to keep this from catching phpunit exceptions as well so do instance checks on it. */ // @codeCoverageIgnoreStart if ($e instanceof PHPUnit_Framework_Error || $e instanceof PHPUnit_Framework_Exception) { throw $e; } // @codeCoverageIgnoreEnd throw new DateTimeWrapperException($e->getMessage(), $e->getCode(), $e); } } } Expected result: ---------------- php should not throw Exception directly but rather an exception that extends Exception. Actual result: -------------- php throws Exception in places like DateTime and DateTimeZone when it should really throw an exception like DateTimeException instead which would extend Exception. -- Edit bug report at https://bugs.php.net/bug.php?id=64313&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=64313&r=trysnapshot54 Try a snapshot (PHP 5.3): https://bugs.php.net/fix.php?id=64313&r=trysnapshot53 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=64313&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=64313&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=64313&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=64313&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=64313&r=needscript Try newer version: https://bugs.php.net/fix.php?id=64313&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=64313&r=support Expected behavior: https://bugs.php.net/fix.php?id=64313&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=64313&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=64313&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=64313&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=64313&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=64313&r=dst IIS Stability: https://bugs.php.net/fix.php?id=64313&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=64313&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=64313&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=64313&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=64313&r=mysqlcfg