Re: Possibility to add finally to try/catch?

From: Date: Tue, 28 Feb 2012 14:45:59 +0000
Subject: Re: Possibility to add finally to try/catch?
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-58242@lists.php.net to get a copy of this message
In many cases this can be done by refactoring to a method: public function main() { $in = 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=36779 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 = $this->whereTheStuffWent($in); fclose($in); if ($finished) { break; } On Tue, Feb 28, 2012 at 9:25 AM, Paul Dragoonis <dragoonis@gmail.com> wrote: > Tried to do something the other day and had to write something a bit quirky > tht would have been super clean with a finally block. > > +1000 > > On Tue, Feb 28, 2012 at 2:22 PM, Kiall Mac Innes <kiall@managedit.ie> wrote: > >> +1000 >> >> This is a feature that I've always wanted in PHP, My main reason being to >> reduce code duplication. eg >> >> try { >>    $fh = fopen($filename); >> >>    // Do some work on the file + encounter an error. >>    throw new Exception(); >> } catch (Exception $e) { >>    // Log an error or something >> } finally { >>    fclose($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 Python >> > 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 code >> > 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 >> > >> > >> -- Tom Boutell P'unk Avenue 215 755 1330 punkave.com window.punkave.com

« previous php.internals (#58242) next »