> On Oct 24, 2019, at 8:05 AM, Dik Takken <d.tak...@xs4all.nl> wrote:
> I fully agree. However I think the fear of having to add tons of try /
> catch to existing code is exaggerated. I use Python a lot, which 
> quite an exception throwing machine in comparison. Still, my code is not
> cluttered with exception handling.
> 
> So, converting a ton of warnings to exceptions in PHP does not worry me
> at all. I would welcome it, but not as a bulk operation. I agree that we
> need to ask the question for each case. I would still prefer the answers
> to bias towards exceptions though.

It is not a "fear" for some, where fear is something someone worries about that 
might be unfounded. 

For me, at least, it is an annoyance I already have to deal with for those 
things that throw exception, such as DateTime and DateTimeZone.  

So not a fear but a preference not to make existing annoyances any worse than 
they already are.  #jmtcw

> Or maybe introduce a try() function that can wrap calls. Since most - ife
> not all - functions would throw a single type of exception, the
> following would catch that exception and return null in stead:
> 
>  $x = try(fopen('missing.txt', 'r'))

Now that I could really get behind.

However, I might suggest it be handled slightly differently, based on my 
experience with Go (which IMO does not handle in a optimal way.)

Basically you want to be able to do something like this:

if ( $result = try( fopen( 'missing.txt', 'r' ), $handle ) ) {
    error_log( $result->getMessage() );
    return;
}
$contents = fread($handle, filesize($filename));
fclose($handle);

Effectively I think we want to be able to easily tell as an expression whether 
it succeeded for failed.  Try() could returns either the  exception, if thrown, 
or false (or null) if no exception was thrown.  The called function's return 
value would be returned by reference as the second parameter.

> Here, try() would swallow only FileException, other exceptions are still
> thrown. I'm not sure if this construct is worth introducing though, the
> difference compared to a proper try / catch is much smaller.

I would want it to capture every exception. Why not?

-Mike

Reply via email to