PHP Exception handler


Published on

How use PHP Exceptions ?

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

PHP Exception handler

  1. 1. PHP Exception handler <ul><li>Throwing and catching exceptions is the good way to make sure your application doesn't do stuff it isn't supposed to do and leaving your application broken. </li></ul>
  2. 2. ISSUE ?? <ul><li>When you're working on a web application, this might not be your desired action because you don't want to let your visitor know your application is broken. Instead, you want to handle exceptions your own way, by bubbling exceptions upwards so you can manage exceptions in one place. </li></ul>
  3. 3. Bubbling exceptions ?? <ul><li>Bubbling exceptions means that you check in your code fragment for an exception (in your try statement) and throwing this upwards (in your catch statement). </li></ul>
  4. 4. Exmp. <ul><li>class MMMException extends Exception {} </li></ul><ul><li>function sendMailers() </li></ul><ul><li>{ </li></ul><ul><li>try { </li></ul><ul><li>// send MMM Mailers </li></ul><ul><li>} catch (MMMException $e) { </li></ul><ul><li>throw new MMMException($e->getMessage()); </li></ul><ul><li>log_message('error', 'Some happen.'); </li></ul><ul><li>} </li></ul><ul><li>return $this->xmlrpc->send_error_message('001', 'error'); </li></ul><ul><li>} </li></ul>
  5. 5. So Finally ... <ul><li>You can create your exception handler method to handle each exception on it's own and perform a specific action. In this case you might want to log these exceptions, but when accessing databases, files or webservices you might want to send out an e-mail to the administrator or developers. </li></ul>
  6. 6. Wrapping it up ... <ul><li>Throwing a default ‘Exception’ is bad practice if you ever want to act on that specific error scenario. Without extending the Exception class you can only catch all or none. </li></ul><ul><li>New exceptions can be thrown in catch blocks. That way it’s possible to prevent unexpected exception types to cross certain application design boundaries. </li></ul><ul><li>Once class design involves abstract classes or interfaces it is wise to design exception structures as well and organize them in similar layers of abstraction. </li></ul>
  7. 7. Sample code <ul><li>Let's start right to see sample code to emulate exception (Everyone loves code, right?) </li></ul>
  8. 8. <ul><li>At any point where a developer needs to handle the possibility of an exception being thrown he needs to know:- </li></ul><ul><li>What Exceptions can I expect? </li></ul><ul><li>What Exceptions do I plan to catch? </li></ul>
  9. 9. The Basic Exception <ul><li>Let’s assume a XMLRPC-based web-service client class used to submit order-data to a supplier’s web-service. The web-service client throws an exception if required configuration parameters are missing. The actual XMLRPC Client object will be initialized when the first webservice method is called. </li></ul>
  10. 10. The Basic Exception <ul><li>try { </li></ul><ul><li>$client->submitOrder($order); </li></ul><ul><li>} catch (Exception $e) { </li></ul><ul><li>// We need to act on this asap... </li></ul><ul><li>$AppError->register_error($e); </li></ul><ul><li>// Redirect to application error page </li></ul><ul><li>$Redirect->error($e); </li></ul><ul><li>} </li></ul>
  11. 11. The Basic Exception <ul><li>In case of a configuration error a redirect is performed to an application-error page. And (just an example) some mechanism is triggered that starts to bug a (or all) developers that there’s a problem that needs immediate attention. </li></ul>
  12. 12. Knowing what to catch <ul><li>class WsXMLRPC_ClientException extends Exception {} </li></ul><ul><li>class WsXMLRPC_ClientConfigurationException extends WsXMLRPC_ClientException{} </li></ul><ul><li>class WsXMLRPC_ClientConnectionException extends WsXMLRPC_ClientException{} </li></ul>
  13. 13. class WsXMLRPC_Client { // ... private function initXMLRPC_Client() { if (!is_null($this->XMLRPC_Client)) { return; } if (!$this->config->searchAgent) { throw new WsXMLRPC_ClientConfigurationException( 'Configuration error' ); } try { $this->XMLRPC_Client = new XMLRPC_Client( $this->config->searchAgent, array('exceptions'=>1) ); } catch (XMLRPC_Fault $e) { throw new WsXMLRPC_ClientConnectionException( 'Cannot load SearchAgent: '.$this->config->searchAgent ); } } }
  14. 14. Sample Code ... <ul><li>$client = new WsXMLRPC_Client($config); </li></ul><ul><li>try { </li></ul><ul><li>$client->submitOrder($order); </li></ul><ul><li>} catch (WsXMLRPC_ClientConnectionException $e) { </li></ul><ul><li>// store the order in a queue to be processed later </li></ul><ul><li>$Order->queue(); </li></ul><ul><li>$Redirect->Page('OrderQueued', $order); </li></ul><ul><li>} catch (Exception $e) { </li></ul><ul><li>// Catch everything, also WsXMLRPC_ClientConfigurationException </li></ul><ul><li>// We need to act on this asap... </li></ul><ul><li>$AppError->register_error($e); </li></ul><ul><li>// Redirect to application error page </li></ul><ul><li>$Redirect->error($e); </li></ul><ul><li>} </li></ul>
  15. 15. Exception in batch job <ul><li>do { </li></ul><ul><li>$retry = false; </li></ul><ul><li>try { </li></ul><ul><li>$bool = // return true if send mail </li></ul><ul><li>if ($bool === false) { </li></ul><ul><li>throw new MailSendException(); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>catch (MailSendException $e) { </li></ul><ul><li>if (Not found SMTP Detail) { </li></ul><ul><li>echo &quot;$mailerid SMTP Details&quot; .&quot;ERROR. &quot;; </li></ul><ul><li>} </li></ul><ul><li>else { </li></ul><ul><li>if (Connection timeout error) { </li></ul><ul><li>echo &quot;connection time out error&quot; .&quot;$mailerid. &quot;; </li></ul><ul><li>} </li></ul><ul><li>else { </li></ul><ul><li>echo &quot;Unknown error attempting to access &quot; .&quot;$mailerid. &quot;; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>$retry = true; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>while ($retry); </li></ul>
  16. 16. [email_address]