Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:62705 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 94598 invoked from network); 3 Sep 2012 05:33:29 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 3 Sep 2012 05:33:29 -0000 Authentication-Results: pb1.pair.com header.from=lester@lsces.co.uk; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=lester@lsces.co.uk; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain lsces.co.uk from 213.123.26.187 cause and error) X-PHP-List-Original-Sender: lester@lsces.co.uk X-Host-Fingerprint: 213.123.26.187 c2beaomr09.btconnect.com Received: from [213.123.26.187] ([213.123.26.187:3714] helo=mail.btconnect.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 76/80-25891-72144405 for ; Mon, 03 Sep 2012 01:33:28 -0400 Received: from host81-138-11-136.in-addr.btopenworld.com (EHLO _10.0.0.5_) ([81.138.11.136]) by c2beaomr09.btconnect.com with ESMTP id IWI04559; Mon, 03 Sep 2012 06:33:24 +0100 (BST) Message-ID: <50444120.40407@lsces.co.uk> Date: Mon, 03 Sep 2012 06:33:20 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:13.0) Gecko/20120604 Firefox/13.0 SeaMonkey/2.10 MIME-Version: 1.0 To: PHP internals References: <50443843.9030004@lerdorf.com> In-Reply-To: <50443843.9030004@lerdorf.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Mirapoint-IP-Reputation: reputation=Fair-1, source=Queried, refid=tid=0001.0A0B0301.50444122.0025, actions=tag X-Junkmail-Premium-Raw: score=7/50, refid=2.7.2:2012.9.3.50632:17:7.944, ip=81.138.11.136, rules=__MOZILLA_MSGID, __HAS_MSGID, __SANE_MSGID, __FW_1LN_BOT_MSGID, __HAS_FROM, __USER_AGENT, __MIME_VERSION, __TO_MALFORMED_2, __BOUNCE_CHALLENGE_SUBJ, __BOUNCE_NDR_SUBJ_EXEMPT, __SUBJ_ALPHA_END, __CT, __CT_TEXT_PLAIN, __CTE, __ANY_URI, __URI_NO_MAILTO, __URI_NO_WWW, __CP_URI_IN_BODY, BODY_ENDS_IN_URL, BODY_SIZE_4000_4999, __MIME_TEXT_ONLY, RDNS_GENERIC_POOLED, HTML_00_01, HTML_00_10, BODY_SIZE_5000_LESS, RDNS_SUSP_GENERIC, RDNS_SUSP, BODY_SIZE_7000_LESS X-Junkmail-Status: score=10/50, host=c2beaomr09.btconnect.com X-Junkmail-Signature-Raw: score=unknown, refid=str=0001.0A0B0208.50444124.012E:SCFSTAT14830815,ss=1,re=-4.000,fgs=0, ip=0.0.0.0, so=2011-07-25 19:15:43, dmn=2011-05-27 18:58:46, mode=multiengine X-Junkmail-IWF: false Subject: Re: [PHP-DEV] Exceptions Vs Errors From: lester@lsces.co.uk (Lester Caine) Rasmus Lerdorf wrote: > On 09/01/2012 06:39 PM, Anthony Ferrara wrote: >> So, while I know there's some discontent about having the core raise >> exceptions, let me ask the question differently: >> >> Without moving to exceptions, how could the current error and error >> handling mechanisms be improved to make the scenario I posted above easier. >> To make handling errors just as easy as ignoring them (or preferably >> easier)...? >> >> Thoughts? > > I think there are other options here. Our current error handling isn't > all that different from how it would be if everything was an exception. > Chances are that common code isn't going to distinguish between the > exceptions anyway and they will end up just doing: > > try { > file_put_contents(...); > } catch (Exception $e) { > error_log($e->getMessage()); > } > > Then the more aware folks will be checking their error logs and noticing > certain errors they may want to handle better and they add explicit > catches for this: > > try { > file_put_contents(...); > } catch (NotWritableException $e) { > page_an_op(); > } catch (Exception $e) { > error_log($e->getMessage()); > } > > In our current system the before and after would be: > > file_put_contents(...); > > And when the same aware person notices a specific condition they want to > handle they change it to: > > if(!is_writable(...)) page_an_op(); > else file_put_contents(); > > The Exception case runs extra code only when the condition happens while > our current system typically has the check before trying to do the > operation so there is an extra cost for each op, but there are cases > where you really don't want it to even try so there is some advantage > there. Of course a similar check can be added before the call in the > Exception case. > > My point here is that these two systems aren't actually that different > and the try/catch approach is certainly more verbose. I would also hate > to end up in the situation where most code has a high-level generic > exception handler that spews huge meaningless stack traces as a > catch-all and so far removed from the context of the actual error that > you can't do anything intelligent about it there. > > So how else could you approach it? > > My biggest beef with exceptions is that often you end up catching them a > couple of levels above where the error actually occurred and you don't > have any sort of recovery context at that point. Common Lisp had the > notion of conditions and restarts to address this. I always found them a > bit cumbersome syntax-wise, but I liked the idea of condition objects > carrying the context of the error. A condition is similar to an > exception in some ways except it carries contextual data about the error > and it isn't necessarily exceptional/fatal if not handled. This > particular characteristic of condition objects appeal to me for PHP > because it would allow us to migrate everything to conditions, not just > the fatal errors, without breaking everything. > > The second part of the Lisp approach is the restart. If you choose to > handle the condition you can also choose to try to gracefully recover by > passing the condition object which includes the context of the error to > an appropriate restart function that lives closer to the code that > actually caused the error. This split allows you to have catch-all type > of condition handlers without losing the ability to do context-aware > error recovery. > > I would love to see a proposal from someone who understands > condition-restart systems inside out for how they might be applied to PHP. Thanks Rasmus ... I think that wraps the current state of affairs up nice and succinctly. Certainly the 'inconsistent' level of exception states are part of the problem here and why personally I'd rather loose them than add more ... try/catch always seems like 'I can't be bothered so just do this if you can' where it would be much tidier if file_put_contents(); was written so it simply finished with an error if it has a problem? -- Lester Caine - G8HFL ----------------------------- Contact - http://lsces.co.uk/wiki/?page=contact L.S.Caine Electronic Services - http://lsces.co.uk EnquirySolve - http://enquirysolve.com/ Model Engineers Digital Workshop - http://medw.co.uk Rainbow Digital Media - http://rainbowdigitalmedia.co.uk