Req #78357 [NEW]: DateInterval throwing more specific Exception

From: Date: Wed, 31 Jul 2019 11:40:01 +0000
Subject: Req #78357 [NEW]: DateInterval throwing more specific Exception
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-222024@lists.php.net to get a copy of this message
From: ed at mention-me dot com Operating system: All PHP version: Irrelevant Package: Date/time related Bug Type: Feature/Change Request Bug description:DateInterval throwing more specific Exception Description: ------------ Apologies if that has already been considered, but I think we'd benefit from throwing a more specific exception from a DateInterval constructor. We have a lot of code that uses DateInterval strings that are either hardcoded strings or constants. As per, https://www.php.net/manual/en/dateinterval.construct.php, if the interval is invalid, an Exception is thrown. We know it's unlikely in most cases. It'd be great if instead a different, more specific, exception could be thrown - e.g. InvalidDateIntervalException. It would make it clearer to see what the possible output paths from the code are, and make it easier (for a human) to parse the code. It also makes it easier to write error handling (Catch blocks) which are specific to this scenario. In certain circumstances, we know the exception can't be thrown (e.g. because the DateInterval is hardcoded/a constant) and so seeing an Exception being thrown is unhelpful. We currently do one of the following: - Add @throws Exception to our function, which makes it unclear as to what the causes of the Exception could be - Add `try { new DateInterval("P1D"); } catch (Exception $e) { return null; //or similar }` which isn't particularly nice as it makes the code quite verbose - And I've considered using a throw new DateIntervalException($e) in the catch block above, but we've not done it yet. The latter would be preferred option, but it makes the code verbose and it'd be awesome if the language could support it directly. Thanks -- Edit bug report at https://bugs.php.net/bug.php?id=78357&edit=1 -- Fix committed: https://bugs.php.net/fix.php?id=78357&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=78357&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=78357&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=78357&r=needscript Try newer version: https://bugs.php.net/fix.php?id=78357&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=78357&r=support Expected behavior: https://bugs.php.net/fix.php?id=78357&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=78357&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=78357&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=78357&r=globals PHP version support discontinued: https://bugs.php.net/fix.php?id=78357&r=phptooold Daylight Savings: https://bugs.php.net/fix.php?id=78357&r=dst IIS Stability: https://bugs.php.net/fix.php?id=78357&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=78357&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=78357&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=78357&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=78357&r=mysqlcfg

« previous php.bugs (#222024) next »