PHP Exception handler 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.
ISSUE ?? 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.
Bubbling exceptions ?? 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).
Exmp. class MMMException extends Exception {} function sendMailers() { try { // send MMM Mailers } catch (MMMException $e) { throw new MMMException($e->getMessage()); log_message('error', 'Some happen.'); } return $this->xmlrpc->send_error_message('001', 'error'); }
So Finally ... 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.
Wrapping it up ... 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. New exceptions can be thrown in catch blocks. That way it’s possible to prevent unexpected exception types to cross certain application design boundaries. 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.
Sample code Let's start right to see sample code to emulate exception (Everyone loves code, right?)
At any point where a developer needs to handle the possibility of an exception being thrown he needs to know:- What Exceptions can I expect? What Exceptions do I plan to catch?
The Basic Exception 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.
The Basic Exception try { $client->submitOrder($order); } catch (Exception $e) { // We need to act on this asap... $AppError->register_error($e); // Redirect to application error page $Redirect->error($e); }
The Basic Exception 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.
Knowing what to catch class WsXMLRPC_ClientException extends Exception {} class WsXMLRPC_ClientConfigurationException extends WsXMLRPC_ClientException{} class WsXMLRPC_ClientConnectionException extends WsXMLRPC_ClientException{}
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 ); } } }
Sample Code ... $client = new WsXMLRPC_Client($config); try { $client->submitOrder($order); } catch (WsXMLRPC_ClientConnectionException $e) { // store the order in a queue to be processed later $Order->queue(); $Redirect->Page('OrderQueued', $order); } catch (Exception $e) { // Catch everything, also WsXMLRPC_ClientConfigurationException // We need to act on this asap... $AppError->register_error($e); // Redirect to application error page $Redirect->error($e); }
Exception in batch job  do { $retry = false; try { $bool = // return true if send mail  if ($bool === false) { throw new MailSendException(); } } catch (MailSendException $e) { if (Not found SMTP Detail) { echo "$mailerid SMTP Details" ."ERROR.\n"; } else { if (Connection timeout error) { echo "connection time out error" ."$mailerid.\n"; } else { echo "Unknown error attempting to access " ."$mailerid.\n"; } } $retry = true; } } while ($retry);
[email_address]

More Related Content

RTF
Triggers-Sequences-SQL
PPT
Exception handling in asp.net
PPT
Dynamic Web Pages Ch 3 V1.0
PPTX
Exception handling in asp.net
PPTX
Exception handling in ASP .NET
PPTX
Handling error & exception in php
PPT
Exceptions in c++
PPTX
javaScript tutorial
Triggers-Sequences-SQL
Exception handling in asp.net
Dynamic Web Pages Ch 3 V1.0
Exception handling in asp.net
Exception handling in ASP .NET
Handling error & exception in php
Exceptions in c++
javaScript tutorial

What's hot (20)

PPT
Exception handling
PPT
Exception handling
PPT
Exception handling and templates
PPTX
Exception Handling in C++
PPT
e computer notes - Creating views
PPTX
C++ ala
PPTX
Angular Mini-Challenges
PPTX
exception handling in cpp
PPTX
Form Validation
PPTX
Exception handling c++
PDF
Exception Handling in the C++ Constructor
PDF
Error handling in XPages
PPTX
Rethrowing exception- JAVA
PPTX
JAVA SWITCH STATEMENT
PDF
Logical Expressions in C/C++. Mistakes Made by Professionals
PDF
Exception handling
PPT
Exception handling
PDF
Declarative presentations UIKonf
KEY
Cramp websockets
PDF
Exceptions ref
 
Exception handling
Exception handling
Exception handling and templates
Exception Handling in C++
e computer notes - Creating views
C++ ala
Angular Mini-Challenges
exception handling in cpp
Form Validation
Exception handling c++
Exception Handling in the C++ Constructor
Error handling in XPages
Rethrowing exception- JAVA
JAVA SWITCH STATEMENT
Logical Expressions in C/C++. Mistakes Made by Professionals
Exception handling
Exception handling
Declarative presentations UIKonf
Cramp websockets
Exceptions ref
 
Ad

Viewers also liked (14)

PDF
Avangate transition to_saa_s_-_whitepaper
PPTX
Surf Pitch Deck
PPT
Social Networking Media
PPTX
Gesl rainbow plus 13 presentation
PDF
5 Software Niche Affiliate Success Stories
ODP
High Performance Web Sites
PPT
Web Performance Tips
PPT
Web application security
PPTX
Conversion Rate Optimization
PPT
Code Review
PPT
Is it time to start using HTML 5
PPT
Character Encoding issue with PHP
PPT
5 Guaranteed Methods to Increase Your Software Business’ Revenue
PPT
How PHP Works ?
Avangate transition to_saa_s_-_whitepaper
Surf Pitch Deck
Social Networking Media
Gesl rainbow plus 13 presentation
5 Software Niche Affiliate Success Stories
High Performance Web Sites
Web Performance Tips
Web application security
Conversion Rate Optimization
Code Review
Is it time to start using HTML 5
Character Encoding issue with PHP
5 Guaranteed Methods to Increase Your Software Business’ Revenue
How PHP Works ?
Ad

Similar to PHP Exception handler (20)

PDF
Php exceptions
PDF
Effective PHP. Part 6
PDF
Object Oriented PHP - PART-2
PDF
Introduction to php exception and error management
PDF
Elegant Ways of Handling PHP Errors and Exceptions
PDF
Nikola Poša "Handling exceptional conditions with grace and style"
KEY
Code Fast, Die Young, Throw Structured Exceptions
PDF
Dealing with not so exceptional exceptions
PPTX
object oriented programming in PHP & Functions
PDF
TDC 2015 - POA - Trilha PHP - Shit Happens
PDF
Zend VMにおける例外の実装
KEY
Code Fast, die() Early, Throw Structured Exceptions
PDF
Les exceptions, oui, mais pas n'importe comment
PDF
Zend VMにおける例外の実装
PPT
Exceptions irst
PPTX
Coding for Scale and Sanity
PDF
Exception Handling: Designing Robust Software in Ruby (with presentation note)
PDF
Exceptions in PHP
PPTX
Training material exceptions v1
Php exceptions
Effective PHP. Part 6
Object Oriented PHP - PART-2
Introduction to php exception and error management
Elegant Ways of Handling PHP Errors and Exceptions
Nikola Poša "Handling exceptional conditions with grace and style"
Code Fast, Die Young, Throw Structured Exceptions
Dealing with not so exceptional exceptions
object oriented programming in PHP & Functions
TDC 2015 - POA - Trilha PHP - Shit Happens
Zend VMにおける例外の実装
Code Fast, die() Early, Throw Structured Exceptions
Les exceptions, oui, mais pas n'importe comment
Zend VMにおける例外の実装
Exceptions irst
Coding for Scale and Sanity
Exception Handling: Designing Robust Software in Ruby (with presentation note)
Exceptions in PHP
Training material exceptions v1

Recently uploaded (20)

PDF
Convolutional neural network based encoder-decoder for efficient real-time ob...
PDF
sbt 2.0: go big (Scala Days 2025 edition)
PDF
Enhancing plagiarism detection using data pre-processing and machine learning...
PDF
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
PPT
Galois Field Theory of Risk: A Perspective, Protocol, and Mathematical Backgr...
PDF
Getting started with AI Agents and Multi-Agent Systems
DOCX
search engine optimization ppt fir known well about this
PDF
Accessing-Finance-in-Jordan-MENA 2024 2025.pdf
PDF
UiPath Agentic Automation session 1: RPA to Agents
PDF
sustainability-14-14877-v2.pddhzftheheeeee
PDF
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
PPTX
The various Industrial Revolutions .pptx
PDF
A proposed approach for plagiarism detection in Myanmar Unicode text
PDF
Flame analysis and combustion estimation using large language and vision assi...
PDF
Statistics on Ai - sourced from AIPRM.pdf
PDF
Produktkatalog für HOBO Datenlogger, Wetterstationen, Sensoren, Software und ...
PDF
Improvisation in detection of pomegranate leaf disease using transfer learni...
PDF
Developing a website for English-speaking practice to English as a foreign la...
PDF
Zenith AI: Advanced Artificial Intelligence
PPTX
AI IN MARKETING- PRESENTED BY ANWAR KABIR 1st June 2025.pptx
Convolutional neural network based encoder-decoder for efficient real-time ob...
sbt 2.0: go big (Scala Days 2025 edition)
Enhancing plagiarism detection using data pre-processing and machine learning...
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
Galois Field Theory of Risk: A Perspective, Protocol, and Mathematical Backgr...
Getting started with AI Agents and Multi-Agent Systems
search engine optimization ppt fir known well about this
Accessing-Finance-in-Jordan-MENA 2024 2025.pdf
UiPath Agentic Automation session 1: RPA to Agents
sustainability-14-14877-v2.pddhzftheheeeee
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
The various Industrial Revolutions .pptx
A proposed approach for plagiarism detection in Myanmar Unicode text
Flame analysis and combustion estimation using large language and vision assi...
Statistics on Ai - sourced from AIPRM.pdf
Produktkatalog für HOBO Datenlogger, Wetterstationen, Sensoren, Software und ...
Improvisation in detection of pomegranate leaf disease using transfer learni...
Developing a website for English-speaking practice to English as a foreign la...
Zenith AI: Advanced Artificial Intelligence
AI IN MARKETING- PRESENTED BY ANWAR KABIR 1st June 2025.pptx

PHP Exception handler

  • 1. PHP Exception handler 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.
  • 2. ISSUE ?? 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.
  • 3. Bubbling exceptions ?? 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).
  • 4. Exmp. class MMMException extends Exception {} function sendMailers() { try { // send MMM Mailers } catch (MMMException $e) { throw new MMMException($e->getMessage()); log_message('error', 'Some happen.'); } return $this->xmlrpc->send_error_message('001', 'error'); }
  • 5. So Finally ... 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.
  • 6. Wrapping it up ... 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. New exceptions can be thrown in catch blocks. That way it’s possible to prevent unexpected exception types to cross certain application design boundaries. 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.
  • 7. Sample code Let's start right to see sample code to emulate exception (Everyone loves code, right?)
  • 8. At any point where a developer needs to handle the possibility of an exception being thrown he needs to know:- What Exceptions can I expect? What Exceptions do I plan to catch?
  • 9. The Basic Exception 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.
  • 10. The Basic Exception try { $client->submitOrder($order); } catch (Exception $e) { // We need to act on this asap... $AppError->register_error($e); // Redirect to application error page $Redirect->error($e); }
  • 11. The Basic Exception 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.
  • 12. Knowing what to catch class WsXMLRPC_ClientException extends Exception {} class WsXMLRPC_ClientConfigurationException extends WsXMLRPC_ClientException{} class WsXMLRPC_ClientConnectionException extends WsXMLRPC_ClientException{}
  • 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. Sample Code ... $client = new WsXMLRPC_Client($config); try { $client->submitOrder($order); } catch (WsXMLRPC_ClientConnectionException $e) { // store the order in a queue to be processed later $Order->queue(); $Redirect->Page('OrderQueued', $order); } catch (Exception $e) { // Catch everything, also WsXMLRPC_ClientConfigurationException // We need to act on this asap... $AppError->register_error($e); // Redirect to application error page $Redirect->error($e); }
  • 15. Exception in batch job do { $retry = false; try { $bool = // return true if send mail if ($bool === false) { throw new MailSendException(); } } catch (MailSendException $e) { if (Not found SMTP Detail) { echo "$mailerid SMTP Details" ."ERROR.\n"; } else { if (Connection timeout error) { echo "connection time out error" ."$mailerid.\n"; } else { echo "Unknown error attempting to access " ."$mailerid.\n"; } } $retry = true; } } while ($retry);