PHP Exceptions
An exception is unexpected program result that can be handled by the program itself.
Examples of exception include trying to open a file that does not exist.
PHP Error handling:
When an error occurs, PHP displays the error message in the web browser with information relating to the error that occurred.
Three ways of commonly used methods to handle errors.
- Die statements– the die function combines the echo and exit function in one.
- Custom error handlers – these are user defined functions that are called whenever an error occurs.
- PHP error reporting – the error message depending on your PHP error reporting settings.
Difference between Errors and Exception
- Exceptions are thrown and intended to be caught while errors are generally irrecoverable.
- Exceptions are handled in an object oriented way. The table below shows the exception object methods
Method | Description | Example |
---|---|---|
getMessage() | Displays the exception's message | <?php echo $e->getMessage(); ?> |
getCode() | Displays the numeric code that represents the exception | <?php echo $e->getCode(); ?> |
getFile() | Displays the file name and path where the exception occurred | <?php echo $e->getFile(); ?> |
getLine() | Displays the line number where the exception occurred | <?php echo $e->getLine(); ?> |
getTrace() | Displays an array of the backtrace before the exception | <?php print_r( $e->getTrace()); ?> |
getPrevious() | Displays the previous exception before the current one | <?php echo $e->getPrevious(); ?> |
getTraceAsString() | Displays the backtrace of the exception as a string instead of an array | <?php echo $e->getTraceAsString(); ?> |
__toString() | Displays the entire exception as a string | <?php echo $e->__toString(); ?> |
Throwing an Exception
The throw statement allows a user defined function or method to throw an exception
<?php
throw new Exception("This is an exception Example");
?>
Example - throwing an exception
The try...catch Statement
To avoid the error from the Example above, we can use the try...catch statement to catch exceptions and continue the process.
try {
code that can throw exceptions
} catch(Exception $e) {
code that runs when an exception is caught
}
Show a message when an exception is thrown:
The try...catch...finally Statement
This statement can be used to catch exceptions. Code in the finally block will always run regardless of whether an exception was caught.
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
}
When an exception is thrown and then indicate that the process has ended:
Example - try catch finally Statement
The Exception Object
The Exception Object contains information about the error or unexpected behaviour that the function encountered.
new Exception(message, code, previous)
Parameter Values
Parameter | Description |
---|---|
message | Optional. A string describing why the exception was thrown |
Code | Optional. An integer that can be used to easily distinguish this exception |
previous | Optional. If this exception was thrown in a catch block of another exception, it is recommended to pass that exception into this parameter |
Methods
When catching an exception, the following table shows some of the methods that can be used to get information about the exception.
Method | Description |
---|---|
getMessage() | Returns a string describing why the exception was thrown |
getPrevious() | If this exception was triggered by another one. |
getCode() | Returns the exception code |
getFile() | Returns the full path of the file |
getLine() | Returns the line number of the line |
Example
Output information about an exception that was thrown:
Multiple Exceptions
Multiple exception use multiple try catch blocks to handle the thrown exceptions. Multiple exceptions are useful when;
- You want to display a customized message depending on the exception thrown
- You want to perform a unique operation depending on the exception thrown
We will modify the code that divides a number by the passed in denominator.
Expect two types of exceptions to occur;
- Division by zero
- Division by a negative number
The PHP built in Exception class is used to throw exceptions.
We will create two classes that extend the exception class and use them to throw exceptions.