# Using less generic exceptions for dates

- Thread: https://externals.io/message/119019
- Original archive: https://news-web.php.net/php.internals/119019
- Started by Vincent Langlet on 2022-11-21T11:34:10+00:00
- Messages: 4
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #119019 — Vincent Langlet — 2022-11-21T11:34:10+00:00

URL: https://externals.io/message/119019#119019

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 ?

---

## #119020 — Derick Rethans — 2022-11-21T12:36:01+00:00

URL: https://externals.io/message/119019#119020 · In reply to: #119019

>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

---

## #119022 — tim@bastelstu.be — 2022-11-22T08:53:41+00:00

URL: https://externals.io/message/119019#119022 · In reply to: #119020

Hi


> > […]
> 
> I think this is a good idea and will add it to my todo list to investigate.
> 

I agree that this is a good idea and would like to further recommend not 
having *just one* Exception, but several Exceptions to allow further 
differentiating the cause, e.g.

class MalformedDateStringException extends DateException {}
class UnrepresentableDateException extends DateException {}

Related PR of mine to add the Exception hierarchy for PHP 8.2's 
ext/random: https://github.com/php/php-src/pull/9220

Best regards
Tim Düsterhus

---

## #119057 — Derick Rethans — 2022-11-30T09:26:55+00:00

URL: https://externals.io/message/119019#119057 · In reply to: #119020

> 
> > […]
> 
> I think this is a good idea and will add it to my todo list to 
> investigate.


I have now made an RFC for this:
https://wiki.php.net/rfc/datetime-exceptions

cheers,
Derick

-- 
https://derickrethans.nl | https://xdebug.org | https://dram.io

Author of Xdebug. Like it? Consider supporting me: https://xdebug.org/support
Host of PHP Internals News: https://phpinternals.news

mastodon: @derickr@phpc.social @xdebug@phpc.social
twitter: @derickr and @xdebug
