Edit report at https://bugs.php.net/bug.php?id=64313&edit=1
ID: 64313 Comment by: mattsch at gmail dot com Reported by: mattsch at gmail dot com Summary: DateTime & DateTimeZone constructs should emit DateTimeException Status: Open Type: Feature/Change Request Package: Date/time related PHP Version: 5.3.22 Block user comment: N Private report: N New Comment: Forget the $format part of the test script. I changed the class slightly to show a simplified form of the problem. Previous Comments: ------------------------------------------------------------------------ [2013-02-27 18:46:31] mattsch at gmail dot com 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 this bug report at https://bugs.php.net/bug.php?id=64313&edit=1