PHP 5 Error Handling

by Ben Chivers
Article Rating:  Star rating: 1 Star rating: 0 Star rating: 1 Star rating: 2 Star rating: 3 
Vote for Article:

Those web developers who have had previous experience of developing applications in C++, Java or C# will be familiar with error capturing and handling, PHP 5 provides the basic principles of error handling like these other advanced languages.


Error Handling in PHP 4

Previously in old versions of PHP, i.e. PHP 4, the way of handling errors is very much similar to that of C in which return values from functions are checked for validity and the programmer can decide what todo if the execution of that statement returns true or false. The drawbacks of this method of error handling is that the code can be difficult to read as it usually ends up in displaying more lines of code than what is needed and ends up with multiple nested if statements when compared to PHP 5 error handling.


PHP 5 Error Handling Solution

To overcome these drawbacks of constantly checking return values from functions, PHP 5 has built-in error handling capabilities where PHP will “try” to execute the code and any errors which occur during the execution of that code will be “caught” to gracefully handle the execution of the rest of the program. This type of implementation of error handling is known as exceptions.


What are Exceptions in PHP 5?

The main principle of exceptions is to allow error capturing. When error handling in PHP 5 is implemented we use try, catch and finally keywords to respectively execute code, catch exceptions which may occur, and finally execute any code regardless of any errors or not. Exceptions allow PHP 5 program to gracefully recover when major errors occur rather than crashing the entire program. They also allow PHP programmers to handle errors in a safe-way and to execute specific sections of code under certain conditions. For instance, you may be coding a function routine to write/read from a file, in the event of PHP 5 failing to obtain a file handle to the actual file an exception will be thrown stating an error message and the cause, this can be caught in your routine to prevent your program from crashing. You can handle this error by either displaying a “friendly” error message to the user, gracefully exit the program, execute specific code on the event occurring or execute specific code on the event occurring or not.


Why handle Program Errors?

Handling errors within your programs whether they are web based or application based is a very important feature to implement for numerous reasons. Gracefully handling errors can ensure the security within your programs to ensure no fatal errors are displayed to the end user when your PHP script throws an error during execution. Your PHP script could quite easily display authentication information, public IP addresses and ports for which are used for connecting to databases, this type of information just makes things easier for hackers on your website. Gracefully handling exceptions when these circumstances occur is very important to ensure both the security of your PHP script, website and web server from hackers. Error handling allows us to capture complex error messages in which we can attain certain information from what has happened during the capture of an exception. This information can be recorded to an error log of the application and a “friendly” error message can be displayed to the user about what has happened rather than displaying information which could potentially be a security risk. Error handling also allows us to execute fail-safe scenarios in the event an exception occurring. If an exception is caught we can decide whether to execute specific code under certain conditions depending on what the exception was. This give us complete control over our program and what can and can’t be executed in the event of a specific exception occurring. For instance, in the event of a MySQL query failing to execute we can catch the exception if one is thrown and decide whether to close or keep the connection open.


Benefits of PHP 5 Exceptions Error Handling

Through the introduction of exceptions in PHP 5 the following benefits can be identified:

  • Reduces the number of lines within the code to make the code easier to debug and read by other developers;
  • Exceptions which aren’t caught within the code which they were thrown allows the exception to move up a level within the call stack, allowing exceptions to be caught elsewhere;
  • Improves manageability for large-scale applications and frameworks – some areas of the code may not have any direct handling for exceptions which may be thrown and can be caught further-up in the program without the fear of the program crashing ungracefully;
  • Error handling in PHP 5 can be completely trivial, where it is possible to execute a number of different code routines under certain conditions and instances.

Drawbacks of PHP 5 Exceptions Error Handling

Although there are many benefits to PHP 5 exception handling, there is also drawbacks to handling errors this way:

  • Error handling in PHP 5 can sometimes make the program even more difficult to read, as exceptions can “bubble-up” through the program architecture. For instance, an exception maybe thrown within a class but may not be caught until two levels up (i.e. an additional two base classes);
  • PHP 5 code will not be compatible with earlier version of PHP e.g. PHP 4;
  • Exceptions are more focused towards object-orientated programming (OOP) rather than procedural code. Programmers may find it more difficult to handle errors with exceptions within procedural code than simply checking the return value of a function.

System Exceptions

PHP 5 doesn’t throw an exception whenever there is a problem within your code. Exceptions are never thrown within procedural code, but are strictly reserved for objects. Even when your using exception handling within your code, there are only two instances when they are thrown by default, these are as follows:

  • Within constructor code of an object;
  • Problems within extensions that are used within your code, e.g. serious problems with connecting a MySQL database using the MySQLi class.

User Exceptions

System exceptions do not allow us to catch many exceptions to add any relevance to error handling with our programs, therefore it is possible to create your own exceptions and throw these within your class methods. For instance, if you are developing a user area for your website and you are checking the validity of the usernames to ensure they are available you could throw an exception within your method to indicate that it is unavailable. The exception could contain a user friendly message which is displayed to the user.


Conclusion

This article has described many benefits of using exception handling over the native way of checking the return values from functions. Whenever you develop classes within your program you should aim to use exception handling within your code, but if you are merely doing procedural functions within your web-based scripts then this probably wouldn’t be the best way to go about it and you should stick to the old way of checking values from functions. Although there are drawbacks to exception handling, the benefits out-weigh the pitfalls under most object-orientated programs.