PHP 5 Exception Handling

In the previous tutorial, we covered error handling. Now the question arise, what is the difference between, Exception and an Error.

With PHP 5 a new object oriented way of handling errors was introduced, which is called Exception.

Exception handling is used to handle errors and redirect the course of code execution when it occurs, unlike errors where the code execution stops with an error message displayed on the screen.


Using try and catch block

The code which can lead to exception or error is enclosed within try block, if no exception occur the code is executed normally while in case of exception, the code execution exits the try block and enters the catch block.

Following is the syntax for using try and catch for exception handling,

<?php
    
try {
    //code goes here that could lead to an exception
}
catch (Exception $e) {
    //exception handling code goes here
}
    
?>

throw Exception

We can manually trigger an exception if required using the throw keyword. Exception is a PHP class which is the parent class for all exception classes in PHP.

To throw an exception we have to create an object of the exception class and then use the throw keyword to trigger that exception.

Let's take an example:

<?php
    // funciton declaration
    function triggerException() {
        // using throw keyword
        throw new Exception("Manually triggering exception...");
    }
    
?>

If we call the above function triggerException() then it will throw exception.

Now we can call this function from within a try block and handle the exception in the catch() block, like this:

<?php
    
try {
    // calling the function
    triggerException();
}
catch (Exception $e) {
    echo "Oops! Some unexpected error occured...";
}
    
?>

Oops! Some unexpected error occured...

Inside the catch block, we can also get the message from the exception object using the getMessage() method.


Custom Exception Class

We can create custom exception class by extending the Exception class provided by PHP.

Below we have a an example for custom exception class:

<?php
    // custom exception class
    class StudytonightException extends Exception {
    
        // define constructor
        function __construct() {
            // we can even take arguments if we want
        }
        
        // we can define class methods if required
        
    }
    
?>

Custom exception classes are useful when you have requirements for custom error handling, for example logging error in database etc.


Handling Multiple Exceptions

If a piece of code can throw different types of exceptions and based on the type of exception we have to perform some action, in such a situation we can have multiple catch blocks:

<?php
    
try {
    // calling the function
    triggerException();
}
catch (StudytonightException $e) {
    // do something here...
}
catch (Exception $e) {
    echo "Oops! Some unexpected error occured...";
}
    
?>

A few important points to remember when handling multiple exceptions using multiple catch blocks:

  1. catch block handling child class of Exception class must be placed above the catch block handling the Exception class. Or in other words, Exception class handling catch block should be kept at last.
  2. catch block handling the Exception class can handle other excpetions as well, as all the exception classes are child class of Exception class.