NYCPHP Meetup

NYPHP.org

[nycphp-talk] "Finally" missing in PHP...

John Campbell jcampbell1 at gmail.com
Tue Aug 5 11:51:31 EDT 2008


On Tue, Aug 5, 2008 at 11:14 AM, Justin Dearing <zippy1981 at gmail.com> wrote:
> So your saying finally could be implemented as
>
> if($e != null){
>   //finally stuff
> }
>

Not quite.  A finally block is supposed to execute no matter whether
an exection occurs and regardless of whether it is caught.

It is more akin to

try {
doStuff();
} catch (Exception $e) {

}
// finally code here
if($e)
  throw $e

So imagine we have something like

try{
mysql_query("ALTER TABLE foo DISABLE KEYS")
// a bunch of queries
} finally {
mysql_query("ALTER TABLE foo ENABLE KEYS")
}

This needs to be rewritten as:
try{
mysql_query("ALTER TABLE foo DISABLE KEYS")
// a bunch of queries
} catch (Exception $e) {

}
mysql_query("ALTER TABLE foo ENABLE KEYS")
if($e)
  throw $e


This pattern quickly becomes a mess if you want to catch certain
Exceptions but not others.

-John C.



More information about the talk mailing list