Re: php exception handling

From: Date: Sun, 11 Oct 2009 23:15:21 +0000
Subject: Re: php exception handling
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-298841@lists.php.net to get a copy of this message
On Sunday 11 October 2009 6:09:46 pm Tommy Pham wrote: > Lars, > > Here's a pseudo code: > > try { > if (!$valid) { > throw new Exception("Test failed."); > } else { > // do something > } > } catch (Exception $e) { > // set/print a user friendly message to send to output > > // set a detailed message using debug_backtrace() or any other > information relevant for easier troubleshooting // log and/or email > detailed message > } > > Regards, > Tommy Actually the else clause is not necessary. That's one of the nice things about exceptions. If you throw an exception, processing jumps to the appropriate catch and never returns. try { // Do normal stuff. if (!$valid) { throw new Exception('OMG!'); } // Do more normal stuff. } catch (Exception $e) { // Print user friendly message. // Log detailed information or whatever you're going to do. } -- Larry Garfield larry@garfieldtech.com

« previous php.general (#298841) next »