Re: php exception handling

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxxxxxxxx

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux