PHP Exception Handling
An exception is a signal that indicates some sort of exceptional event or error has occurred. Exceptions can be caused due to various reasons, for example, database connection or query fails, file that you’re trying to access doesn’t exist, and so on.
PHP provides a powerful exception handling mechanism that allows you to handle exceptions in a graceful way. As opposed to PHP’s traditional error-handling system, exception handling is the object-oriented method for handling errors, which provides a more controlled and flexible form of error reporting. The exception model was first introduced in PHP 5.
Using Throw and Try…Catch Statements
In an exception-based approach, program code is written in a try block, an exception can be thrown using the throw statement when an exceptional event occurs during the execution of code in a try block. It is then caught and resolved by one or more catch blocks.
The following example demonstrates how exception handling works:
You might be wondering what this code was all about. Well, let’s go through each part of this code one by one for a better understanding.
Explanation of Code
The PHP’s exception handling system has basically four parts: try, throw, catch, and the Exception class. The following list describes how each part exactly works.
- The division() function in the example above checks if a divisor is equal to zero. If it is, an exception is thrown via PHP’s throw statement. Otherwise, this function performs the division using given numbers and displays the result.
- Later, the division() function is called within a try block with different arguments. If an exception is generated while executing the code within
the try block, PHP stops execution at that point and attempts to find the corresponding catch block. If it is found, the code within that catch block is executed, if not, a fatal error is generated. - The catch block typically catches the exception thrown within the try block and creates an object ($e) containing the exception information. The error message from this object can be retrieved using the Exception’s getMessage() method.The PHP’s Exception class also provides getCode(), getFile(), getLine() and getTraceAsString() methods that can be used to generate detailed debugging information.
The Exception’s constructor optionally takes an exception message and an exception code. While the exception message is typically used to display generic information on what went wrong, the exception code can be used to categorize the errors. The exception code provided can be retrieved later via Exception’s getCode() method.
Defining Custom Exceptions
You can even define your own custom exception handlers to treat different types of exceptions in a different way. It allows you to use a separate catch block for each exception type.
You can define a custom exception by extending the Exception class because Exception is the base class for all exceptions. The custom exception class inherits all the properties and methods from PHP’s Exception class. You can also add your custom methods to the custom exception class. Let’s check out the following example:
In the above example, we’ve derived two new exception
classes: EmptyEmailException, and InvalidEmailException from the Exception base class. Multiple catch blocks are used to display different error messages, depending on the type of exception generated.
Since these custom exception classes inherit the properties and methods from the Exception class, so we can use the Exception’s class methods like getMessage(), getLine(), getFile(), etc. to retrieve error information from the exception object.
Setting a Global Exception Handler
As we’ve discussed earlier in this chapter if an exception is not caught, PHP generates a Fatal Error with an “Uncaught Exception …” message. This error message may contain sensitive information like file name and line number where the problem occurs. If you don’t want to expose such information to the user, you can create a custom function and register it with the set_exception_handler() function to handle all uncaught exceptions.
If you liked our content PHP Exception Handling, then you may also like Regular Expression
Leave a Reply