Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:62070 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 77633 invoked from network); 6 Aug 2012 18:58:49 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 6 Aug 2012 18:58:49 -0000 Authentication-Results: pb1.pair.com smtp.mail=ajf@ajf.me; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=ajf@ajf.me; sender-id=pass Received-SPF: pass (pb1.pair.com: domain ajf.me designates 64.22.89.133 as permitted sender) X-PHP-List-Original-Sender: ajf@ajf.me X-Host-Fingerprint: 64.22.89.133 oxmail.registrar-servers.com Linux 2.6 Received: from [64.22.89.133] ([64.22.89.133:57416] helo=oxmail.registrar-servers.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id C1/84-03102-8E310205 for ; Mon, 06 Aug 2012 14:58:49 -0400 Received: from [192.168.0.200] (5ad4bfa0.bb.sky.com [90.212.191.160]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by oxmail.registrar-servers.com (Postfix) with ESMTPSA id 0D22DC30004; Mon, 6 Aug 2012 14:58:44 -0400 (EDT) Message-ID: <502013C8.70400@ajf.me> Date: Mon, 06 Aug 2012 19:58:16 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:14.0) Gecko/20120714 Thunderbird/14.0 MIME-Version: 1.0 To: Stas Malyshev CC: Amaury Bouchard , Ferenc Kovacs , PHP Internals References: <501F46BE.4040407@sugarcrm.com> <501FAD8F.2070600@ajf.me> <5020118B.8070409@sugarcrm.com> In-Reply-To: <5020118B.8070409@sugarcrm.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Error handling brainstorming From: ajf@ajf.me (Andrew Faulds) 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/