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

Reply via email to