Re: Error handling brainstorming

From: Date: Mon, 06 Aug 2012 18:58:16 +0000
Subject: Re: Error handling brainstorming
References: 1 2 3 4 5  Groups: php.internals 
Request: Send a blank email to internals+get-62070@lists.php.net to get a copy of this message
On 06/08/12 19:48, Stas Malyshev wrote:
Hi!
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
No, they mean you need to *always* try/catch since you have to means to check if the downstream function can throw and exception or maybe call some other function that throws and exception. Exceptions propagate. In Java, you have compiler-controlled throws (which doesn't work in 100% of cases either - I regularly get exception messages from Eclipse, and this one is written by people with great knowledge of Java) - in PHP, you will have nothing. It's one thing when the code writes a warning and returns null when something unexpected happens, another when it throws and exception and you *have* to know which kind it will be and catch it - or just catch everything and ignore it (because you have no idea what it might be) and - check some success value then! Why do you have to *always* try/catch? You're making the assumption you need to check for and catch every error. You don't, unless you're working with something like networking. Exceptions mean that an exception, an error that infrequently pops up, but occasionally enough that it matters, can be caught and dealt with. Other ones you don't have to account for, because execution will just stop right there. You don't need to bother checking the return value.
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.
How it makes it harder? If you forget to check the result, you'll as well forget to try/catch. It just makes the consequences worse - and remember, this all happens in runtime! With exceptions, your code stops running then and there. Without them, with an unchecked return value, your code keeps going until it does something stupid, which can be quite irritating to debug.
-- Andrew Faulds http://ajf.me/

« previous php.internals (#62070) next »