PHP has an exception model similar to that of other programming languages. An exception can be thrown, and caught (“catched”) within PHP. Code may be surrounded in a try block, to facilitate the catching of potential exceptions. Each try must have at least one corresponding catch or finally block.

If an exception is thrown and its current function scope has no catch block, the exception will “bubble up” the call stack to the calling function until it finds a matching catch block. All finally blocks it encounters along the way will be executed. If the call stack is unwound all the way to the global scope without encountering a matching catch block, the program will terminate with a fatal error unless a global exception handler has been set.

The thrown object must be an instanceof Throwable. Trying to throw an object that is not will result in a PHP Fatal Error. As of PHP 8.0.0, the throw keyword is an expression and may be used in any expression context. In prior versions it was a statement and was required to be on its own line.

Let’s make a code to see exceptions in a PHP file.

File – code1.php

<?php
echo 10/0;
?>
output

Note: from the upper code, you see ‘Division by zero’ exception in PHP code.

But we can handle exceptions using try-catch method.

try

PHP Try Block The try bock contain a block of program code which exception may occur. A try block always followed by a catch block which handles the exception.

catch

A catch block defines how to respond to a thrown exception. A catch block defines one or more types of exception or error it can handle, and optionally a variable to which to assign the exception. (The variable was required prior to PHP 8.0.0.) The first catch block a thrown exception or error encounters that matches the type of the thrown object will handle the object.

Multiple catch blocks can be used to catch different classes of exceptions. Normal execution (when no exception is thrown within the try block) will continue after that last catch block defined in sequence. Exceptions can be thrown (or re-thrown) within a catch block. If not, execution will continue after the catch block that was triggered.

finally

A finally block may also be specified after or instead of catch blocks. Code within the finally block will always be executed after the try and catch blocks, regardless of whether an exception has been thrown, and before normal execution resumes.

One notable interaction is between the finally block and a return statement. If a return statement is encountered inside either the try or the catch blocks, the finally block will still be executed. Moreover, the return statement is evaluated when encountered, but the result will be returned after the finally block is executed. Additionally, if the finally block also contains a return statement, the value from the finally block is returned.

throw

The throw statement allows a user defined function or method to throw an exception. When an exception is thrown, the code following it will not be executed.

If an exception is not caught, a fatal error will occur with an “Uncaught Exception” message.

Let’s write a code, to show the exceptions handling using try-catch block.

File – code2.php

<?php
function divide($dividend, $divisor) {
  if($divisor == 0) {
    throw new Exception();
  }
  return $dividend / $divisor;
}
 
try {
  echo divide(5, 0);
} catch(Exception $e) {
  echo “error!! occured !! “;
}
?>
output

Let’s write a code, to show try-catch-finally block usages

Note: finally block executed every time through error occurred!

file – code3.php

<?php
try{
    echo 10/0;
}catch(Exception $e){
    echo $e;
}
finally{
    echo “<br> finally block executed!”;
    echo “<br> <font color=’green’ size=’20px’>
    finally block executed, though error occured!
    </font>”;
}
?>
output