php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #78357 DateInterval throwing more specific Exception
Submitted: 2019-07-31 11:40 UTC Modified: 2022-06-05 14:25 UTC
Votes:2
Avg. Score:4.0 ± 1.0
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:1 (50.0%)
From: ed at mention-me dot com Assigned: derick (profile)
Status: Wont fix Package: Date/time related
PHP Version: Next major version OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: ed at mention-me dot com
New email:
PHP Version: OS:

 

 [2019-07-31 11:40 UTC] ed at mention-me dot com
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


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-09-07 15:21 UTC] girgias@php.net
-Operating System: All +Operating System: -PHP Version: Irrelevant +PHP Version: Next major version -Assigned To: +Assigned To: derick
 [2019-09-07 15:21 UTC] girgias@php.net
Assigning to derick as he's the maintainer of the datetime extension.
 [2022-06-05 14:25 UTC] derick@php.net
-Status: Assigned +Status: Wont fix
 [2022-06-05 14:25 UTC] derick@php.net
I am marking this as "won't fix". If you are still interested in having this feature, please file a new issue at https://github.com/php/php-src/issues/ with a well defined and explained use case.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 16:01:28 2024 UTC