Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:58255 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 15979 invoked from network); 28 Feb 2012 16:48:32 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 28 Feb 2012 16:48:32 -0000 Authentication-Results: pb1.pair.com smtp.mail=tom@punkave.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=tom@punkave.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain punkave.com designates 209.85.210.170 as permitted sender) X-PHP-List-Original-Sender: tom@punkave.com X-Host-Fingerprint: 209.85.210.170 mail-iy0-f170.google.com Received: from [209.85.210.170] ([209.85.210.170:46029] helo=mail-iy0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 68/93-34356-D550D4F4 for ; Tue, 28 Feb 2012 11:48:31 -0500 Received: by iaeh11 with SMTP id h11so3074479iae.29 for ; Tue, 28 Feb 2012 08:48:27 -0800 (PST) Received-SPF: pass (google.com: domain of tom@punkave.com designates 10.50.216.231 as permitted sender) client-ip=10.50.216.231; Authentication-Results: mr.google.com; spf=pass (google.com: domain of tom@punkave.com designates 10.50.216.231 as permitted sender) smtp.mail=tom@punkave.com Received: from mr.google.com ([10.50.216.231]) by 10.50.216.231 with SMTP id ot7mr23599637igc.8.1330447707577 (num_hops = 1); Tue, 28 Feb 2012 08:48:27 -0800 (PST) MIME-Version: 1.0 Received: by 10.50.216.231 with SMTP id ot7mr19191001igc.8.1330447707472; Tue, 28 Feb 2012 08:48:27 -0800 (PST) Received: by 10.231.108.135 with HTTP; Tue, 28 Feb 2012 08:48:27 -0800 (PST) In-Reply-To: References: <01a901ccf622$0645f230$12d1d690$@alliantinternet.com> Date: Tue, 28 Feb 2012 11:48:27 -0500 Message-ID: To: Kiall Mac Innes Cc: Arvids Godjuks , Richard Lynch , internals@lists.php.net Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Gm-Message-State: ALoCoQkpBjX4xFq/wWkCq10ltnj2TYS+A6oba41fmbTBTY826Jbilca7pq4HonxgyQbgnS0fF2fb Subject: Re: [PHP-DEV] Possibility to add finally to try/catch? From: tom@punkave.com (Tom Boutell) On Tue, Feb 28, 2012 at 11:32 AM, Kiall Mac Innes wrot= e: > Yes, You could abstract the try/catch into a new (and un-needed) function > to try and emulate the behavior of finally.. Unless, for example, you > re-throw the exception after logging in the catch. 'finally' doesn't run for stuff that throws an exception not caught in the try { } block, or an exception thrown again in the catch { } block - does it? I would hope not, since that means "something this block of code did not anticipate at all - another sort of exceptional situation altogether" and really should not run any more local code, nothing until and unless there is a catch block somewhere further up that does catch that exception. Assuming I'm right and 'finally' runs only for normal completion and for exceptions actually caught by the catch { } block attached to the same try { }, then my refactoring still behaves exactly like 'finally'. I am not convinced the function is unnecessary, either. Decomposition is usually a good idea (: > substitute for a native language construct > > try { > =A0 =A0$fh =3D fopen($filename); > > =A0 =A0// Do some work on the file + encounter an error. > =A0 =A0throw new Exception(); > } catch (Exception $e) { > =A0 =A0$log->add(Log::ERROR, "Something went wrong", $e); > > =A0 =A0throw $e; > } finally { > =A0 =A0fclose($fh); > } > > Thanks, > Kiall > > > On Tue, Feb 28, 2012 at 4:12 PM, Arvids Godjuks wrote: > >> It's quite hard to think of a compelling example. Usually you just >> write the code and realize that "Yea, a 'finally' statement would make >> a lot of sense here". >> I believe it's the kind of functionality that you learn to use in >> time, just like __set, __get, __sleep, __wakeup. If first two are no >> brainier these days, the last two to me where "wtf? does anyone use >> that? Where I need them at all?!". It was like that for 3-4 years. >> Until I got the code witch actually made an active use of these two >> and I understood the purpose. >> Same goes to finally. I remember using in JavaScript a few times, helped= a >> lot. >> And it just makes sense to add to complete the try {} catch {} finally >> {} template. >> My 2 cents. >> >> 2012/2/28 Richard Lynch : >> > On Tue, February 28, 2012 8:22 am, Kiall Mac Innes wrote: >> >> +1000 >> >> >> >> This is a feature that I've always wanted in PHP, My main reason bein= g >> >> to >> >> reduce code duplication. eg >> >> >> >> try { >> >> =A0 =A0 $fh =3D fopen($filename); >> >> >> >> =A0 =A0 // Do some work on the file + encounter an error. >> >> =A0 =A0 throw new Exception(); >> >> } catch (Exception $e) { >> >> =A0 =A0 // Log an error or something >> > + =A0 =A0 =A0 if ($fh) fclose($fh); //many PHP file errors NULL out th= e $fh >> > + =A0} >> > - > } finally { >> > - > =A0 =A0 fclose($fh); >> > - > } >> > >> > Another non-compelling example... >> > >> > Still not saying "finally" is a bad idea. >> > >> > Just want a compelling use case before I would vote... >> > >> > -- >> > brain cancer update: >> > http://richardlynch.blogspot.com/search/label/brain%20tumor >> > Donate: >> > >> https://www.paypal.com/cgi-bin/webscr?cmd=3D_s-xclick&hosted_button_id= =3DFS9NLTNEEKWBE >> > >> > >> > >> > -- >> > PHP Internals - PHP Runtime Development Mailing List >> > To unsubscribe, visit: http://www.php.net/unsub.php >> > >> >> -- >> PHP Internals - PHP Runtime Development Mailing List >> To unsubscribe, visit: http://www.php.net/unsub.php >> >> --=20 Tom Boutell P'unk Avenue 215 755 1330 punkave.com window.punkave.com