Featured Posts

Writing Website Scrapers in PHPWriting Website Scrapers in PHP This article discusses about how to write a website scraper using PHP for web site data extraction. The concepts taught can be applied and programmed in Java, C#, etc. Basically any language that has a...

Readmore

12 common programming mistakes to avoid12 common programming mistakes to avoid Programming is an art and science and like all art and science the only way to learn is from mistakes. I have made many... and I would like to share with you the mistakes that I have made over my journey...

Readmore

7 habits of highly effective freelance programmers7 habits of highly effective freelance programmers I have developed these based on my freelancing experience. Though I have discontinued freelancing, but would like to share my practices with you. These are basic practices and have been developed over...

Readmore

  • Prev
  • Next

PHP 5 Tutorial - Handling Exceptions in PHP5

Posted on : 14-02-2008 | By : admin | In : PHP, PHP Class Examples, PHP Code Examples, PHP Tutorials, PHP5 OOPS Tutorials

2

If you're new here, you may want to subscribe to my Newsletter. Thanks for visiting!

In this tutorial we will cover the following:

  • What is an exception?
  • The use of a try…catch block
  • Anatomy of PHP5 Exception class
  • Extending the Exception class
  • A note on unhanded exceptions

What is an exception?

An exception is a logical/system error that occurs during the normal execution of a script. The exception could either be raised by the system or the program itself it the exception cannot be handled and the caller script/function needs to be informed about the same.

The use of a try…catch block

PHP5 introduces the try…catch block to trap exceptions. Look at the example below.

 
try {
   check();
}
catch(Exception $e) {
   echo "Message : " . $e->getMessage();
   echo "Code : " . $e->getCode();
}
 
function check() {
   if(some error condition) {
      throw new Exception("Error String",Error Code);
   }
}

In the above example, the method check() is called between the try {} block. The try{} block is the area where you will place your code that could raise an exception. Below the try{} block is the catch() {} block. The catch block expects the Exception type of object as a parameter. Within the catch() {} block you will place your logic to either fix the issue or log the error.

In the function check(), we raise an Exception using the ‘throw’ keyword. The statement following ‘throw’ is the syntax of creating a new object of Exception type. The exception class accepts two parameters. The left parameter is a string that is the error message and the right parameter is the integer error code that you wish to assign to that error.




Anatomy of PHP5 Exception class

 
class Exception {
	protected $message;
	protected $code;
	protected $file;
	protected $line;
 
	private $string;
	private $trace;
 
	public function __construct($message = null, $code = 0);
	public function __toString();
 
	final public function getCode();
	final public function getMessage();
	final public function getFile();
	final public function getLine();
	final public function getTrace();
	final public function getTraceAsString();
	final private __clone();
}

In the above example, except for __construct and __toString(), no other method can be overridden as all other methods are ‘final’.

Related Reading:

Extending the Exception class

You can also extend the exception class as follows:

 
class CustomerException extends Exception {
 
  public function __construct($message = null, $code = 0) {
 
    $t_message = "Exception raised in CustomerException ";
    $t_message .= "with message : " . $message;
 
    parent::__construct($t_message, $code);
 
    }
 
}
 
 
function testException() {
 
	throw new CustomerException("CustomerException has been raised",101);
 
}
 
try {
	testException();
}
catch(CustomerException $e) {
	echo "Error Message : " $e->getMessage();
	echo "Error Code : " $e->getCode();
}

Output:

Error Message : CustomerException has been raised
Error Code : 101

Related Reading:
PHP5 Tutorial - Inheritance




A note on unhanded exceptions

All un-handled exceptions are passed up the function stack order till the time either a try…catch block is not made available. If a try…catch block is unavailable it is passed to the PHP core and the program execution stops there.

Please feel free to write back for any clarification.

Subscribe to my free newsletter:

Your email:

 

Related Posts on PHP5 Tutorial - Object Oriented Programming (OOPS)

  1. PHP5 Tutorial - Learn to create a PHP5 Class
  2. PHP5 Tutorial - Learn to Create a PHP5 Class Object
  3. PHP5 Tutorial - Defining Attributes of a PHP5 Class
  4. PHP5 Tutorial - Defining Methods of a PHP5 Class
  5. PHP5 Tutorial - Creating a PHP5 Constructor __construct()
  6. PHP5 Tutorial OOPS - Creating a PHP5 Destructor __destruct()
  7. PHP5 Tutorial OOPS - PHP5 Class Access Specifiers - public, private and protected
  8. PHP5 Tutorial - $this variable explained
  9. PHP5 Tutorial - instanceOf Operator Explained
  10. PHP5 Tutorial - Defining Class Constants
  11. PHP5 Tutorial - Inheritance
  12. PHP5 Tutorials - Abstract Class and Interface
  13. PHP5 OOPS Tutorials - Polymorphism
  14. PHP 5 Tutorials - Static Data Members and Methods
  15. PHP 5 Tutorial - Final Class and Methods
  16. PHP5 Tutorial - Magic Methods - __toString() method
  17. PHP5 Tutorial - Magic Methods - __get() and __set()
  18. PHP5 Tutorial - Magic Methods - __isset() and __unset()
  19. PHP5 Tutorial - Magic Methods - __call() method
  20. PHP5 Tutorial - Magic Methods - __autoload() method
  21. PHP5 Tutorial - Magic Methods - __sleep() and __wakeup()
  22. PHP5 Tutorial - Magic Methods - __clone() method

Comments (2)

PHP 5 Tutorial - Handling Exceptions in PHP5 | Geek Files…

This PHP5 tutorial discusses the use and implementation of PHP5 Exceptions. Contains indepth notes on Exceptions and how to extend the Exception class and use it effectively…

STUMBLED!

I am hoping my host switched to php5 soon.

VOTED for you at:
http://www.newsdots.com/tutorials/handling-exceptions-in-php5-geek-files/

Write a comment

Enter this code