Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:62076 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 98468 invoked from network); 7 Aug 2012 01:16:23 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 7 Aug 2012 01:16:23 -0000 Authentication-Results: pb1.pair.com header.from=ircmaxell@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=ircmaxell@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.216.170 as permitted sender) X-PHP-List-Original-Sender: ircmaxell@gmail.com X-Host-Fingerprint: 209.85.216.170 mail-qc0-f170.google.com Received: from [209.85.216.170] ([209.85.216.170:41484] helo=mail-qc0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id E9/19-03102-66C60205 for ; Mon, 06 Aug 2012 21:16:22 -0400 Received: by qcmt36 with SMTP id t36so2528421qcm.29 for ; Mon, 06 Aug 2012 18:16:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=pqj54hKxl0hsuZl8Zs6T1164IN5J2eGWXOCYzVFcKPw=; b=pSQU3cBPby2WCiR/EMYH3H3v5f6wg8siCb+qA39MSkYAo7iB5wFg7PWCzIMfn0hxWi ASHaP4V1qssacp/2AyvG3RvtEHAJ6cGazz4Obohob/4g5dxJ0XhMnf3ThXbhDpWhPCTp WdfDLgGIhVqy+OXukTx8Qwac9b45kEzMWcFwMLHiFwT5cFBLIhN4R7C3IDSNJ4MPH32H ffICIIIJEgX+KQ+UDmwBUM4rFgp++LCdcoVldcDsoV0lrctNLMwOQwexQ7Cc/DEGe5L/ siv76EbtOqUGSQgWbtWuKWj+uHQdkZ+TBAfnClNjR6E4mOgNcN366LNKaH4XGoLGEG/S VQxw== MIME-Version: 1.0 Received: by 10.224.1.130 with SMTP id 2mr20838503qaf.61.1344302180036; Mon, 06 Aug 2012 18:16:20 -0700 (PDT) Received: by 10.229.182.4 with HTTP; Mon, 6 Aug 2012 18:16:19 -0700 (PDT) In-Reply-To: References: <501F46BE.4040407@sugarcrm.com> <50200FBD.2010506@sugarcrm.com> Date: Mon, 6 Aug 2012 21:16:19 -0400 Message-ID: To: Levi Morrison Cc: Stas Malyshev , Ferenc Kovacs , PHP Internals Content-Type: multipart/alternative; boundary=20cf3074b8fee9710a04c6a2bd13 Subject: Re: [PHP-DEV] Error handling brainstorming From: ircmaxell@gmail.com (Anthony Ferrara) --20cf3074b8fee9710a04c6a2bd13 Content-Type: text/plain; charset=ISO-8859-1 Levi et al: On Mon, Aug 6, 2012 at 8:55 PM, Levi Morrison wrote: > >> Because checking that the returned variable is `!== FALSE` is *way* > >> better than throwing an exception, right? > > > > Yes, it is. You can control it, unlike the exception which you can not, > > unless, again, you wrap everything into try/catch on every kind of > > exception possible. > > > >> This type of thing is one of the main reasons I like PDO more than > >> MySQLi. In MySQLi I'm constantly checking return values. In PDO I > >> just wrap it all up one try/catch. It's not like if my query fails > >> I'm going to try a different one. Most of the time it's just logging > >> that something went wrong and reporting it upstream somehow. > > > > You are using exceptions for normal flow control. It is not what > > exceptions should be used for. They are called exceptions for a reason. > > > > > It *should* be an exceptional situation if my database queries don't > work properly. I've tested my code locally, on a staging environment > and sometimes even ran tests on the live server. It is absolutely > 100% an exception if something goes wrong, my friend. > > Opening a file? I have to agree that it should not throw an exception. > I was replying more to explain the benefit of exceptions than to > refute that one possible use-case. It should absolutely throw an exception. If you're opening a file, and the file doesn't exist (in read mode), that's definitely an exceptional circumstance. If it's not, you should be checking for existence first (via file_exists() or is_readable(), etc)... The only situations I consider non-exceptional in terms of PHP errors are those that don't alter the flow via error return. So if fopen errors, you can't just fread the return value. You *need* to change your execution flow based on that error. Therefore, it is exceptional. However, cases where you can logically continue on would not be exceptional. Cases such as substr($string, 100) on a 2 character string, currently raise a warning, but getting an empty string back from it allows you to continue on. The rest of your code won't be effected by the error in a significant way (since the case where the string is 2 characters is the same as if it's 100 characters). So, as a gross overgeneralization, Can I continue on without **having** to check the return value? Yes: Not an exceptional circumstance No: An exceptional circumstance Again, as a gross overgeneralization. Not as a hard rule, but as a quick check... Anthony --20cf3074b8fee9710a04c6a2bd13--