Home > PHP, PHP Class Examples, PHP Code Examples, PHP Tutorials, PHP5 OOPS Tutorials > PHP 5 Tutorial – Handling Exceptions in PHP5

PHP 5 Tutorial – Handling Exceptions in PHP5

February 14th, 2008

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 {
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 {
catch(CustomerException $e) {
	echo "Error Message : " $e->getMessage();
	echo "Error Code : " $e->getCode();


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:

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
  1. April 15th, 2008 at 21:58 | #1


    I am hoping my host switched to php5 soon.

    VOTED for you at:

Comments are closed.