Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:58258 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 20634 invoked from network); 28 Feb 2012 17:03:15 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 28 Feb 2012 17:03:15 -0000 Authentication-Results: pb1.pair.com header.from=kiall@managedit.ie; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=kiall@managedit.ie; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain managedit.ie designates 209.85.214.42 as permitted sender) X-PHP-List-Original-Sender: kiall@managedit.ie X-Host-Fingerprint: 209.85.214.42 mail-bk0-f42.google.com Received: from [209.85.214.42] ([209.85.214.42:49182] helo=mail-bk0-f42.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id ED/94-34356-1D80D4F4 for ; Tue, 28 Feb 2012 12:03:14 -0500 Received: by bkcje9 with SMTP id je9so440623bkc.29 for ; Tue, 28 Feb 2012 09:03:10 -0800 (PST) Received-SPF: pass (google.com: domain of kiall@managedit.ie designates 10.204.154.202 as permitted sender) client-ip=10.204.154.202; Authentication-Results: mr.google.com; spf=pass (google.com: domain of kiall@managedit.ie designates 10.204.154.202 as permitted sender) smtp.mail=kiall@managedit.ie; dkim=pass header.i=kiall@managedit.ie Received: from mr.google.com ([10.204.154.202]) by 10.204.154.202 with SMTP id p10mr9659856bkw.79.1330448590963 (num_hops = 1); Tue, 28 Feb 2012 09:03:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=managedit.ie; s=google; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=jvrDGgegJd74YQOJK5H03t3y36NKgxilbrOGWhZYzoI=; b=RaK4sgKI3IBoz2lpHMOBfpP7ciHK/KRbAHwl7XD0kkemKbPW/D9OA6jmNQ8BHg+SG4 U/RHls02mTo+HiApZYmvwkvCJz2SXYnOdH5Eodopix8wNRkLn1NQYwTm5AgQYBOfuZT+ 1srNnmrPhmQQoK/bDgqOEMEgUXpMoK2jCE6xo= Received: by 10.204.154.202 with SMTP id p10mr7769592bkw.79.1330448590692; Tue, 28 Feb 2012 09:03:10 -0800 (PST) MIME-Version: 1.0 Received: by 10.204.42.74 with HTTP; Tue, 28 Feb 2012 09:02:48 -0800 (PST) In-Reply-To: References: <01a901ccf622$0645f230$12d1d690$@alliantinternet.com> Date: Tue, 28 Feb 2012 17:02:48 +0000 Message-ID: To: Tom Boutell Cc: Arvids Godjuks , Richard Lynch , internals@lists.php.net Content-Type: multipart/alternative; boundary=0015175d07bca3e2bc04ba093390 X-Gm-Message-State: ALoCoQkstxaOndysE2pNoi7uXzQgKdYnS2QwJgfela3mUUqDCSpnT/+eLwhfSH7P/yODCssZW81q Subject: Re: [PHP-DEV] Possibility to add finally to try/catch? From: kiall@managedit.ie (Kiall Mac Innes) --0015175d07bca3e2bc04ba093390 Content-Type: text/plain; charset=ISO-8859-1 An an equivalent snippet of ruby.. $ irb irb(main):001:0> def divide(x, y) irb(main):002:1> begin irb(main):003:2* result = x / y irb(main):004:2> puts "result is", result irb(main):005:2> rescue ZeroDivisionError irb(main):006:2> puts "division by zero!" irb(main):007:2> raise Exception('division by zero') irb(main):008:2> ensure irb(main):009:2* puts "executing finally clause" irb(main):010:2> end irb(main):011:1> end => nil irb(main):012:0> irb(main):013:0* irb(main):014:0* divide(2, 1) result is 2 executing finally clause => nil irb(main):015:0> irb(main):016:0* divide(2, 0) division by zero! executing finally clause <====== 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 wrote: > 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 the > catch block. > > $ python > >>> def divide(x, y): > ... try: > ... result = x / y > ... except ZeroDivisionError: > ... print "division by zero!" > ... raise Exception('division by zero') > ... else: > ... print "result is", result > ... finally: > ... print "executing finally clause" > ... > >>> divide(2, 1) > result is 2 > executing finally clause > >>> > >>> divide(2, 0) > division by zero! > executing finally clause <====== The interesting part > Traceback (most recent call last): > File "", line 1, in > File "", line 6, in divide > Exception: division by zero > >>> > >>> divide("2", "1") > executing finally clause > Traceback (most recent call last): > File "", line 1, in > File "", line 3, in divide > TypeError: unsupported operand type(s) for /: 'str' and 'str' > --0015175d07bca3e2bc04ba093390--