Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:58242 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 90567 invoked from network); 28 Feb 2012 14:46:32 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 28 Feb 2012 14:46: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.213.42 as permitted sender) X-PHP-List-Original-Sender: tom@punkave.com X-Host-Fingerprint: 209.85.213.42 mail-yw0-f42.google.com Received: from [209.85.213.42] ([209.85.213.42:46162] helo=mail-yw0-f42.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 5E/A1-17048-7C8EC4F4 for ; Tue, 28 Feb 2012 09:46:31 -0500 Received: by yhfq11 with SMTP id q11so1196435yhf.29 for ; Tue, 28 Feb 2012 06:46:00 -0800 (PST) Received-SPF: pass (google.com: domain of tom@punkave.com designates 10.50.161.170 as permitted sender) client-ip=10.50.161.170; Authentication-Results: mr.google.com; spf=pass (google.com: domain of tom@punkave.com designates 10.50.161.170 as permitted sender) smtp.mail=tom@punkave.com Received: from mr.google.com ([10.50.161.170]) by 10.50.161.170 with SMTP id xt10mr16774785igb.8.1330440360041 (num_hops = 1); Tue, 28 Feb 2012 06:46:00 -0800 (PST) MIME-Version: 1.0 Received: by 10.50.161.170 with SMTP id xt10mr13767701igb.8.1330440359965; Tue, 28 Feb 2012 06:45:59 -0800 (PST) Received: by 10.231.108.135 with HTTP; Tue, 28 Feb 2012 06:45:59 -0800 (PST) In-Reply-To: References: <01a901ccf622$0645f230$12d1d690$@alliantinternet.com> Date: Tue, 28 Feb 2012 09:45:59 -0500 Message-ID: To: Paul Dragoonis Cc: Kiall Mac Innes , Dmitri Snytkine , internals@lists.php.net Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Gm-Message-State: ALoCoQkcuzof1sCZqO/KOIo1y4tkngjapZk8b2geChdMHtq0+72gYKtWUcFTq2DOHZJ1MwZ5Ciou Subject: Re: [PHP-DEV] Possibility to add finally to try/catch? From: tom@punkave.com (Tom Boutell) In many cases this can be done by refactoring to a method: public function main() { $in =3D fopen('whatever'); $this->whereTheStuffWent($in); fclose($in); } protected function whereTheStuffWent($in) { try { // Do stuff that might throw an exception } catch (Exception $e) { // Log it or something } } The while loop case in https://bugs.php.net/bug.php?id=3D36779 could be dealt with by returning a result from whereTheStuffWent and breaking out if it's set. Not too bad. finally { } might be a little more convenient than this I suppose: $finished =3D $this->whereTheStuffWent($in); fclose($in); if ($finished) { break; } On Tue, Feb 28, 2012 at 9:25 AM, Paul Dragoonis wrote= : > Tried to do something the other day and had to write something a bit quir= ky > tht would have been super clean with a finally block. > > +1000 > > On Tue, Feb 28, 2012 at 2:22 PM, Kiall Mac Innes wro= te: > >> +1000 >> >> This is a feature that I've always wanted in PHP, My main reason being t= o >> reduce code duplication. eg >> >> 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 an error or something >> } finally { >> =A0 =A0fclose($fh); >> } >> >> Thanks, >> Kiall >> >> >> On Tue, Feb 28, 2012 at 2:05 PM, Dmitri Snytkine < >> dsnytkine@ultralogistics.com> wrote: >> >> > >> > This is another feature that we know we can live without >> > but those familiar with languages that have 'finally' like Java or Pyt= hon >> > know that >> > it is very neat feature to have in some situations. >> > >> > Basically the code inside 'finally' is guaranteed to run even if there >> is a >> > 'return' inside try or catch blocks >> > in which case the value to be returned is remembered tempraraly, the c= ode >> > inside finally block executes and then remembered value is returned. >> > >> > >> > Dmitri Snytkine >> > Web Developer >> > Ultra Logistics, Inc. >> > Phone: (888) 220-4640 x 2097 >> > Fax: (888) 795-6642 >> > E-Mail: dsnytkine@ultralogistics.com >> > Web: www.ultralogistics.com >> > >> > "A Top 100 Logistics I.T. Provider in 2011" >> > >> > >> > >> > >> > -- >> > 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