PHP5 Tutorial – exception handling

January 1st, 2011 by Laeeq | 1 comment

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.

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

 

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.

 

You can subscribe to PHPZAG.COM posts by Email

 

Related Topics:

  • Find Time Between Two Dates in PHP
  • Creating Objects in PHP5 Class
  • PHP5 Tutorial – Definition of a class attribute
  • PHP5 Tutorial – Creating a PHP5 Destructor
  • PHP5 Tutorial- Definition of Constructor
  • PHP5 Tutorial – __clone() method
  • PHP5 Tutorial – magic method
  • PHP5 Tutorial – Magic Methods – __autoload() method
  • PHP5 Tutorial – magic method __call()
  • PHP5 Tutorial – Magic Methods – __isset() and __unset()
  • PHP5 Tutorial – get()) and __set() magic method
  • PHP5 Tutorial – __toString() method
  • PHP5 Tutorial – Polymorphism
  • PHP5 Tutorial – abstract class and interface
  • PHP5 Tutorial- Inheritance
  • PHP5 Tutorial – Final Class and Final Method
  • PHP5 Tutorial – Static Data Members and Methods
  • PHP5 Tutorial – Defining Class Constants
  • PHP5 Tutorial – instanceOf Operator
  • PHP5 Tutorial – $this variable
  •