Re: Using less generic exceptions for dates
| From: | Derick Rethans | Date: | Mon, 21 Nov 2022 12:36:01 +0000 |
| Subject: | Re: Using less generic exceptions for dates | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-119020@lists.php.net to get a copy of this message | ||
On 21 November 2022 11:34:10 GMT, Vincent Langlet <mr.vincent.langlet@gmail.com> wrote:
>Hi,
>
>When using json_encode or json_decode with the
>
JSON_THROW_ON_ERROR flag,
>JsonException might be thrown.
>
>When using new DateTime('foo'), a generic
>Exception is thrown.
>Incidentally, I wonder why it's not an
>InvalidArgumentException (but that
>could be another debate).
>
>But my main point is that I think it would be useful to use a specific
>exception
>```
>class DateException extends Exception {}
>```
>- It allows a specific treatment when catching exceptions
>- It allows a specific analysis when using static analysis tools like Psalm
>or PHPStan.
>
>In a general way, I would say that PHP class/method should always use
>scoped Exception instead of generics ones.
>
>I know nothing about how php is implemented but I would say introducing
>DateException shouldn't be too hard and it's BC. What do you think ?
I think this is a good idea and will add it to my todo list to investigate.
cheers
Derick