On Tue, Feb 28, 2012 at 11:32 AM, Kiall Mac Innes <ki...@managedit.ie> 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. 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 { > $fh = fopen($filename); > > // Do some work on the file + encounter an error. > throw new Exception(); > } catch (Exception $e) { > $log->add(Log::ERROR, "Something went wrong", $e); > > throw $e; > } finally { > fclose($fh); > } > > Thanks, > Kiall > > > On Tue, Feb 28, 2012 at 4:12 PM, Arvids Godjuks > <arvids.godj...@gmail.com>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 <c...@l-i-e.com>: >> > 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 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 >> > + if ($fh) fclose($fh); //many PHP file errors NULL out the $fh >> > + } >> > - > } finally { >> > - > 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=_s-xclick&hosted_button_id=FS9NLTNEEKWBE >> > >> > >> > >> > -- >> > 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 >> >> -- Tom Boutell P'unk Avenue 215 755 1330 punkave.com window.punkave.com -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php