Handling Exceptions in PHP5


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 Tutorials – 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.

No comments: