PHP Exceptions

This tutorial teaches you about PHP Exceptions and ways to use them. 

 

PHP Exceptions

Exception is the new object-oriented way of handling errors. It was introduced in PHP 5.

You can use Exception handling to handle errors and redirect the code execution course when it occurs, dissimilar to errors where the code execution stops and an error message is displayed on the screen.

 

Using try and catch block

The try block has the code that may lead to exception or error; in case no exception occurs, then the code is executed normally. Whereas, if exception happens, the code execution will exit the try block and enter into the catch block.

The syntax used for a try and catch for exception handling is as follows.

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

 

throw Exception

You can use the throw keyword to manually trigger an exception if required. Exception is a PHP class. In PHP, it is the parent class for all exception classes.

You must create an exception class's object and then use the throw keyword to trigger that exception for throwing an exception.

Example:

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

 

On calling the above function triggerException(), it will throw exception.

Now let's try calling this function from within a try block and handle the exception in the catch() block, as below:

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

Output:

Oops! Some unexpected error occured...

You may also get the message from the exception object within a catch block by utilizing the getMessage() method.

 

Custom Exception Class

A custom exception class can be created by extending the Exception class provided by PHP.

An example of a custom exception class is mentioned below:

<?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 helpful if you have requirements for custom error handling, like logging errors in databases, etc.

 

Handling Multiple Exceptions

If different types of exceptions can be thrown by a piece of code and you have to perform some actions on the basis of the type of exception, 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...";
}
    
?>

 

Points to remember while handling multiple exceptions utilizing multiple catch blocks:

  • You must place the catch block handling child class of the Exception class above the catch block handling the Exception class. Or, you must keep the Exception class handling catch block at last.
  • Other exceptions can also be handled by catch block handling the Exception class, as all the exception classes are child class of the Exception class.