On 06/08/12 08:43, Amaury Bouchard wrote:
2012/8/6 Stas Malyshev <smalys...@sugarcrm.com>

Exceptions are different from PHP errors. For example, if you try to
open a file and the file isn't there, throwing exception is a very
annoying behavior (yes I know some languages do that, IMO it's wrong).
The reason is that it's pretty normal and within normal set of
situations to which code should be prepared, so you will either have to
obsessively wrap everything with try/catch blocks or do exception typing
like Java does. Both options are quite annoying. I think it's much
better to just open file and check if the result is OK. Converting it to
exception doesn't really improve situation, as if downstream code didn't
handle it the upstream probably won't know what to do with that
exception either. This leads to code like try { whatever(); }
catch(Exception e) {}. I saw tons of that in Java.

Even a simple file opening can fail for different kind of reasons (the file
doesn't exists, it is not readable, on some OS it could be already opened
and thus locked). Most of the time, you don't care the reason, but
sometimes you want to be more precise. With exceptions, we have an elegant
way to manage all failures as a whole, or to differenciate each reason.

But you are right, it could be very annoying to write a lot of try/catch
blocks. Maybe we could think about something inspired from Lua's "protected
call" function [1]. A convenient mean to say «OK, I know this expression
may raise an exception, but I don't care, discard it silently please. I
will check if my variable has been set or not.».

[1] : http://www.lua.org/pil/8.4.html

Personally, I'm used to what other languages like Python do, and I think it makes more sense. Exceptions mean you can try/catch the things your code needs to be prepared for (non-existence, maybe), but other things you haven't considered or are very unlikely, can just not be caught and cause your code to halt execution. And it makes it harder to accidentally avoid checking for errors and then wonder why $file is some NULL or FALSE or other "error" value.

--
Andrew Faulds
http://ajf.me/


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

Reply via email to