RE: [PHP] Exception vs exception

From: Date: Fri, 22 Feb 2008 04:54:06 +0000
Subject: RE: [PHP] Exception vs exception
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-269833@lists.php.net to get a copy of this message
> -----Original Message----- > From: Prabath Kumarasinghe [mailto:prabath321@yahoo.com] > Sent: Thursday, February 21, 2008 11:19 PM > To: php-general@lists.php.net > Subject: [PHP] Exception vs exception > > Hi All > > Recently I'm came across following code snippet > regarding exceptions there I'm executing invalid query > and catch the exception as follows. > > First Approach > ------------------------------------------- > try{ > $result = mysql_query('SELECT * from unknowntable'); > > > }catch(exception $e){ > echo $e->getTrace(); > > } > ------------------------------------------- > > > Second Approach > ------------------------------------------- > try{ > > $result = mysql_query('SELECT * from unknowntable'); > if (!$result) { > throw new MySQLException('Query failed to > execute') > } > > }cathc(MySQLException $e){ > echo $e->getMessage(); > }cathc(Exception $e){ > echo $e->getMessage(); > } > ------------------------------------------- > > Which one is the best approach and efficient and what > the recommended approach. > > Cheers > > Prabath > > > > ___________________________________________________________________________ > _________ > Never miss a thing. Make Yahoo your home page. > http://www.yahoo.com/r/hs Class names are case insensitive, so "Exception" is the same as "exception". Now, about the first approach: Does the mysql extension throw exceptions??? (maybe you just didn't write the throw statement, I presume). About the second approach: What is "catchc"? (I guess it's just a typo). And, what other exceptions are you expecting to be thrown out of the try block to use the second *generic* catch? If you and only you are throwing the custom * MySQLException* exception (which you should code and throw under the proper conditions) why do you need a generic *Exception* catch block? Regards, Rob Andrés Robinet | Lead Developer | BESTPLACE CORPORATION  5100 Bayview Drive 206, Royal Lauderdale Landings, Fort Lauderdale, FL 33308 | TEL 954-607-4207 | FAX 954-337-2695 | Email: info@bestplace.net  | MSN Chat: best@bestplace.net  |  SKYPE: bestplace |  Web: bestplace.biz  | Web: seo-diy.com

« previous php.general (#269833) next »