An exception is an object that represents an error or unexpected behavior in a PHP script.
Many PHP functions and classes throw exceptions.
User-defined functions and classes can also throw exceptions.
Exceptions provide a way to halt a function when it encounters data it cannot process.
The throw
statement enables a user-defined function or method to throw an exception. When an exception is thrown, any code following it will not be executed.
If an exception is not caught, a fatal error with an “Uncaught Exception” message will occur.
Let’s demonstrate throwing an exception without catching it:
<?php function divide($dividend, $divisor) { if($divisor == 0) { throw new Exception(“Division by zero”); } return $dividend / $divisor; } echo divide(5, 0); ?> |
The result will appear as follows:
Fatal error: Uncaught Exception: Division by zero in C:\webfolder\test.php:4 Stack trace: #0 C:\webfolder\test.php(9): divide(5, 0) #1 {main} thrown in C:\webfolder\test.php on line 4 |
To prevent the error shown in the example above, you can use the try…catch statement to catch exceptions and allow the process to continue.
try { code that can throw exceptions } catch(Exception $e) { code that runs when an exception is caught } |
Display a message when an exception is thrown:
<?php function divide($dividend, $divisor) { if($divisor == 0) { throw new Exception(“Division by zero”); } return $dividend / $divisor; } try { echo divide(5, 0); } catch(Exception $e) { echo “Unable to divide.”; } ?> |
The catch block specifies the type of exception to be caught and the variable name used to access the exception. In the example above, the exception type is Exception and the variable name is $e.
The try…catch…finally statement can be used to handle exceptions. Code within the finally block will always execute, whether or not an exception was caught. If a finally block is used, the catch block becomes optional.
try { code that can throw exceptions } catch(Exception $e) { code that runs when an exception is caught } finally { code that always runs regardless of whether an exception was caught } |
Display a message when an exception is thrown, and then indicate that the process has concluded.
<?php function divide($dividend, $divisor) { if($divisor == 0) { throw new Exception(“Division by zero”); } return $dividend / $divisor; } try { echo divide(5, 0); } catch(Exception $e) { echo “Unable to divide. “; } finally { echo “Process complete.”; } ?> |
Output a string regardless of whether an exception was caught.
<?php function divide($dividend, $divisor) { if($divisor == 0) { throw new Exception(“Division by zero”); } return $dividend / $divisor; } try { echo divide(5, 0); } finally { echo “Process complete.”; } ?> |
The Exception
object provides details about the error or unexpected behavior encountered by the function.
new Exception(message, code, previous) |
Parameter |
Description |
message |
Optional: A string explaining the reason the exception was thrown |
code |
Optional: An integer used to uniquely identify this exception among others of the same type |
previous |
Optional: If this exception was thrown within a |
When catching an exception, the following table lists some methods you can use to obtain information about the exception:
Method |
Description |
getMessage() |
Returns a string explaining the reason for the exception being thrown |
getPrevious() |
If this exception was caused by another exception, this method returns the previous exception; otherwise, it returns null. |
getCode() |
Returns the exception code |
getFile() |
Returns the full path of the file where the exception was thrown |
getLine() |
Returns the line number of the code that threw the exception |
Display information about an exception that was thrown:
<?php function divide($dividend, $divisor) { if($divisor == 0) { throw new Exception(“Division by zero”, 1); } return $dividend / $divisor; } try { echo divide(5, 0); } catch(Exception $ex) { $code = $ex->getCode(); $message = $ex->getMessage(); $file = $ex->getFile(); $line = $ex->getLine(); echo “Exception thrown in $file on line $line: [Code $code] $message”; } ?> |