Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:58259 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 22448 invoked from network); 28 Feb 2012 17:18:12 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 28 Feb 2012 17:18:12 -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:63168] helo=mail-iy0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 0D/F4-34356-45C0D4F4 for ; Tue, 28 Feb 2012 12:18:12 -0500 Received: by iaeh11 with SMTP id h11so3111623iae.29 for ; Tue, 28 Feb 2012 09:18:09 -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 ot7mr23786433igc.8.1330449489931 (num_hops = 1); Tue, 28 Feb 2012 09:18:09 -0800 (PST) MIME-Version: 1.0 Received: by 10.50.216.231 with SMTP id ot7mr19333547igc.8.1330449489859; Tue, 28 Feb 2012 09:18:09 -0800 (PST) Received: by 10.231.108.135 with HTTP; Tue, 28 Feb 2012 09:18:09 -0800 (PST) In-Reply-To: References: <01a901ccf622$0645f230$12d1d690$@alliantinternet.com> Date: Tue, 28 Feb 2012 12:18:09 -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: ALoCoQk1jN5qat9weH8AXR3ZIqlxlLQecahjS9AQdqDfa0HxsljkUx3cQgezuZ2JCerZQIIkyNkY Subject: Re: [PHP-DEV] Possibility to add finally to try/catch? From: tom@punkave.com (Tom Boutell) OK, consider my point successfully buried in counterexamples (: On Tue, Feb 28, 2012 at 12:02 PM, Kiall Mac Innes wrot= e: > An an equivalent snippet of ruby.. > > $ irb > irb(main):001:0> def divide(x, y) > irb(main):002:1> =A0 =A0 begin > irb(main):003:2* =A0 =A0 =A0 =A0 result =3D x / y > irb(main):004:2> =A0 =A0 =A0 =A0 puts "result is", result > irb(main):005:2> =A0 =A0 rescue ZeroDivisionError > irb(main):006:2> =A0 =A0 =A0 =A0 puts "division by zero!" > irb(main):007:2> =A0 =A0 =A0 =A0 raise Exception('division by zero') > irb(main):008:2> =A0 =A0 ensure > irb(main):009:2* =A0 =A0 =A0 =A0 puts "executing finally clause" > irb(main):010:2> =A0 =A0 end > irb(main):011:1> end > =3D> nil > irb(main):012:0> > irb(main):013:0* > irb(main):014:0* divide(2, 1) > result is > 2 > executing finally clause > =3D> nil > irb(main):015:0> > irb(main):016:0* divide(2, 0) > division by zero! > executing finally clause=A0<=3D=3D=3D=3D=3D=3D The interesting part > NoMethodError: undefined method `Exception' for main:Object > from (irb):7:in `divide' > from (irb):16 > from :0 > irb(main):017:0> > irb(main):018:0* divide("2", "1") > executing finally clause > NoMethodError: undefined method `/' for "2":String > from (irb):3:in `divide' > from (irb):18 > from :0 > > Thanks, > Kiall > > > > On Tue, Feb 28, 2012 at 4:54 PM, Kiall Mac Innes wro= te: >> >> On Tue, Feb 28, 2012 at 4:48 PM, Tom Boutell wrote: >>> >>> On Tue, Feb 28, 2012 at 11:32 AM, Kiall Mac Innes >>> wrote: >>> > 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. >> >> >> I would indeed expect the finally to run regardless of what happens in t= he >> catch block. >> >> $ python >> >>> def divide(x, y): >> ... =A0 =A0 =A0try: >> ... =A0 =A0 =A0 =A0 =A0result =3D x / y >> ... =A0 =A0 =A0except ZeroDivisionError: >> ... =A0 =A0 =A0 =A0 =A0print "division by zero!" >> ... =A0 =A0 =A0 =A0 =A0raise Exception('division by zero') >> ... =A0 =A0 =A0else: >> ... =A0 =A0 =A0 =A0 =A0print "result is", result >> ... =A0 =A0 =A0finally: >> ... =A0 =A0 =A0 =A0 =A0print "executing finally clause" >> ... >> >>> divide(2, 1) >> result is 2 >> executing finally clause >> >>> >> >>> divide(2, 0) >> division by zero! >> executing finally clause=A0<=3D=3D=3D=3D=3D=3D The interesting part >> Traceback (most recent call last): >> =A0 File "", line 1, in >> =A0 File "", line 6, in divide >> Exception: division by zero >> >>> >> >>> divide("2", "1") >> executing finally clause >> Traceback (most recent call last): >> =A0 File "", line 1, in >> =A0 File "", line 3, in divide >> TypeError: unsupported operand type(s) for /: 'str' and 'str' > > --=20 Tom Boutell P'unk Avenue 215 755 1330 punkave.com window.punkave.com