Errors are resolved by fixing the program. An example of an error would be an infinite loop that never stops executing. 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. This exception can be handled by either creating the file or presenting the user with an option of searching for the file. In this tutorial, you will learn-

Why handle exception? PHP Error handling Error handling examples Difference between Errors and Exception Multiple Exceptions Testing the code

Why handle exception?

	Avoid unexpected results on our pages which can be very annoying or irritating to our end users

	Improve the security of our applications by not exposing information which malicious users may use to attack our applications

	Php Exceptions are used to change the normal flow of a program if any predictable error occurs.

PHP Error handling

When an error occurs, depending on your configuration settings, PHP displays the error message in the web browser with information relating to the error that occurred. PHP offers a number of ways to handle errors. We are going to look at three (3) commonly used methods;

Die statements– the die function combines the echo and exit function in one. It is very useful when we want to output a message and stop the script execution when an error occurs.

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. This method is very useful in development environment when you have no idea what caused the error. The information displayed can help you debug your application.

Error handling examples

Let’s now look at some simple examples with error handling routines. Let’s suppose that we have developed an application that uses text files to store data. We might want to check for the file’s existence before we attempt to read data from it. The code below implements the above example. Assuming you saved the file simple_error.php in phptuts folder, open the URL http://localhost/phptuts/simple_error.php You will get the following results

As you can see from the above results, it makes our application look unprofessional and can be annoying to the user.

We will modify the above code and write an error handler for the application Assuming you saved the above code as error_handling.php, open the URL http://localhost/phptuts/error_handling.php

Note: it’s a good security practice to display a message as the one shown above instead of showing the message like “File not found”. Let’s look at another example that uses a custom error handler. The custom error handler will be set as the default PHP error handling function and will basically display an error number and message. The code below illustrates the implementation of the above example Open the URL http://localhost/phptuts/custom_error_handler.php you will get the following results .

	They allow us to customize the error messages.

	The custom error handler can also include error logging in a file/database, emailing the developer etc.

Let’s now look at the third type of error handling. We will be using the PHP built in function error_reporting function. It has the following basic syntax HERE,

	“error_reporting” is the PHP error reporting function

	“$reporting_level” is optional, can be used to set the reporting level. If no reporting level has been specified, PHP will use the default error reporting level as specified in the php.ini file.

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.This means when an exception is thrown; an exception object is created that contains the exception details.

The table below shows the exception object methods

HERE,

	“throw” is the keyword used to throw the exception

	“new Exception(…)” creates an exception object and passes “This is an exception example “ string as the message parameter.

The above code outputs the following message.

We are now going to look at an example that implements the throw and catch exceptions.

We will modify the above example and include the try, throw and catch. It has the following basic syntax. HERE,

	“try{…}” is the block of code to be executed that could potentially raise an exception

	“catch(Exception $e){…}” is the block of code that catches the thrown exception and assigns the exception object to the variable $e.

The code below shows the basic exception example with the try, throw and catch exception implemented. The program deliberately throws an exception which it then catches. Open the URL http://localhost/phptuts/exception_handling.php You will get the following results.

It’s also possible to create multiple exceptions for one php try statement depending on the type of exception thrown.

See the article on MySQL, PHP data access… for implementation examples of multiple exceptions

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

The flowchart below illustrates the how multiple exceptions work

Let’s look at an example that uses multiple exceptions.

We will modify the code that divides a number by the passed in denominator. We expect two types of exceptions to occur;

	Division by zero

	Division by a negative number

For simplicity’s sake, we will only display the exception type in our catch blocks. 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. The code below shows the implementation.

Testing the code

We will assume you saved multiple_exceptions.php in phptuts folder. Browse to the URL http://localhost/phptuts/multiple_exceptions.php

Switch back to the PHP file and pass -1 as the parameter as shown in the following diagram.

Browse to the URL http://localhost/phptuts/multiple_exceptions.php.

What results do you get? Pass 3 as the parameter. What results do you get?

Summary

	Errors are unexpected results produced by PHP code

	Error handling improves the application performance

	PHP has built in functions that can be used to customize the way PHP reports errors

	Exceptions are like errors, but they can be caught using the catch block when thrown.

	Displaying error messages that show error information is considered a bad security practice.