> On Oct 21, 2019, at 5:10 AM, David Negrier <d.negr...@thecodingmachine.com> 
> wrote:
> 
> Hey list,
> 
> TL;DR:
> 
> I'm considering writing an RFC (and a patch) to turn some warning into
> exceptions in a number of PHP functions.
> I would first like to gather some feedback here.

JMTCW, which is an opinion admittedly probably not shared by most who work with 
PHP, but I find exceptions problematic and try to avoid them whenever possible. 
 I have again included this list of references[1-6] to explain why I found them 
problematic, which I had mentioned just yesterday on an unrelated message to 
this list.

The idea that functions that I could previously dealt with in four lines of 
code:
$filepath = __DIR__ . '/foobar.json';
if (false === ($content = file_get_contents($filepath)) ) {
   error_log( sprintf("failed to open '%s'", $filepath ));
   return false;
}
Will potentially now requires me to add a second brace-enclosed block and two 
more lines of code and force me to handle varying exception classes causes me 
to cringe and will make me want to cry:

$filepath = __DIR__ . '/foobar.json';
try {
   $content = file_get_contents( $filepath );
} catch (Exception $exception) {
   error_log( sprintf( "failed to open '%s'", $filepath ) );
   return false;
}

Since I know that many in the PHP world believe that exceptions are a good 
thing, I understand that I will be unlikely to change everyone's mind and get 
them to start doing error handling as soon as they discover the error rather 
than just throw it and let some other code that doesn't have as much context 
deal with it.  

But my hope is at least PHP won't change to force having to use try/catch for 
practically every function call. Such a change might even be enough to finally 
get me to give up on PHP and move full time to Go because it will make coding 
in PHP so tedious.

For me to support such a change — not that I have a vote — I would ask we add a 
pragma that could be set at the top of every file that specifies that functions 
throw exceptions or return errors, and for BC please make the lack of such 
pragma to default to returning errors.  

Or better yet, accept Benjamin Morel's suggestion to create a brand new API for 
this rather than attempting to fix the existing one.

One of the other. Please.

-Mike
P.S. Frankly — in my perfect world — we would have options to not throw 
Exceptions for everything that currently throws an exception and instead return 
an Error object value that indicates the error.  They could even return the 
Exception — that would work — just don't throw them so I do not have to set up 
a `try{}catch(Execption $e){}` construct.

Just the other day I had to deal with the fact PhpStorm flags functions that 
return Exceptions if I don't handle them, and `new DateTime()` throws an 
Exception because it's optional parameter could cause a failure, even though I 
was not passing in any parameter. I had to wrap in my own `TryCatch()` function 
that basically just buries the Exception to get PhpStorm to stop complaining.

But I do not hold out much hope for having an option to bypass all Exception 
ever being accepted by the PHP community.

[1] http://www.lighterra.com/papers/exceptionsharmful/
[2] https://sidburn.github.io/blog/2016/03/25/exceptions-are-evil
[3] http://xahlee.info/comp/why_i_hate_exceptions.html
[4] https://www.joelonsoftware.com/2003/10/13/13/
[5] https://mortoray.com/2012/04/02/everything-wrong-with-exceptions/
[6] https://www.atlassian.com/blog/archives/exceptions_are_bad

Reply via email to