On Wed, Jun 29, 2022, at 10:42 AM, Guilliam Xavier wrote:
> Hi (note: your message was flagged as spam),
>
>> https://github.com/php/php-src/issues/8843
>
> So I understand it as having a "true code" equivalent for the
> `@throws` phpDoc comment (similar to type declarations for `@param`
> and `@return`)? which would also be checked at run-time?
>
> Just my 2 cents on that (let's call it "exception specification"):
>
>   1. IMHO, it shouldn't be mixed in the return type declaration (with
> a union-like syntax) but separated and independent (e.g. with a
> `throws` keyword, and pipes [or commas] between multiple exception
> classes)
>   2. AFAIK, Java has it (mandatory to compile for "checked
> exceptions", sometimes controversial), C++ used to have it
> (runtime-checked) but dropped it (and introduced `noexcept` instead);
> I don't know of any dynamic language that has it
>
> You would also need to define how it plays with inheritance (and
> reflection), and what exactly should happen when a function throws an
> exception it didn't "declare"...
>
> PS: I also found some old feature requests:
>   - https://externals.io/message/4424
>   - https://bugs.php.net/bug.php?id=42251
>   - https://bugs.php.net/bug.php?id=62404
>   - https://bugs.php.net/bug.php?id=67312
>
> Regards,

Side Note: Please don't top post.

On the subject of checked exceptions, I *highly* recommend this writeup from 
the lead of the Midori language[1].  It's long, but there's a section 
specifically on exceptions (no deep link, just search the page for it) that 
goes into a lot of detail about exceptions, checked exceptions, and the ways in 
which both fail.  It's absolutely worth everyone's time.

The summary for the short-timed is that the only way exceptions work, from a 
reliability standpoint, is if they are 1) Rare and 2) Strictly checked and 3) 
Have really good syntactic shorthand support.  And checked exceptions without 
the other two are a recipe for hell.  Basically, it only works if exceptions 
become an alternate syntax for an Either monad or Go's multi-return.

Since PHP is a long, long way from there, I don't believe checked exceptions 
would be wise, or even slightly a good idea.  Not until/unless it's done as 
part of a global rethink of error handling that reimagines how exceptions and 
errors work at a fundamental level.  And that couldn't be done in a BC way with 
the current practice of "I dunno, throw, YOLO" in PHP exceptions, so they'd 
have to be some other, different channel.  We are definitely not ready for that 
kind of fundamental rethink of how error handling works in PHP.

[1] http://joeduffyblog.com/2016/02/07/the-error-model/

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to