Exception Handling in PHP

  • Filter
  • Time
  • Show
Clear All
new posts

    Exception Handling in PHP

    All object oriented programming languages that I have come across, have offered a special way of handling errors. PHP is no different. Errors in object oriented programming languages are called exceptions. In this article, I introduce you to a tutorial series on Exception Handling in PHP.

    Programming Errors
    There are three types of programming errors. In other words, there are three types of errors that can occur in a program. You have Syntax Errors, Logic Errors and Runtime Errors.

    Syntax Errors
    This is the wrong use of syntax. These errors are wrong statements. When you type a statement, which is wrong, that is a syntax error. Such a statement cannot be executed. For example, in a statement you can type a variable without the $ sign. Under this condition, your program does not work. Depending on how you configure your PHP installation, such an error might be indicated by PHP to the output device just before the program is to be executed, when you give a command to run the program. With a syntax error, the program is not executed. Before PHP code is executed, there is some minimum compilation that takes place. Syntax errors would be spotted by the PHP compiler and reported, and execution (interpretation) of the program will not take place.

    Logic Errors
    In this case, PHP interpreter understands your program very well and it executes the program. However, the program will not do what you wanted it to do. It will do something slightly different or completely different. The fault is yours. For example, a loop that is required to do 10 iterations might do 5 iterations, because you coded it mistakenly to do 5 iterations. Another example is that a loop might iterate infinitely, because the condition you gave for the loop is wrong. Logic Errors occur when the program is being executed (interpreted). The only way to solve this problem is to test your program very well before you hand it over to the customer (who asked for it).

    Runtime Errors
    Runtime errors occur when the program is being executed as a result of the fact that you did not take certain factor into consideration when coding. For example, let us say your code is to divide 8 by some denominator that the user inputs. If the user inputs 2, the division will work, giving you 4 as answer. If the user inputs zero, the division will not work, because 8/0 is undefined. When a runtime error occurs, your program normally crashes (and stop). To solve runtime errors, you have to write code that will prevent the execution of the particular code segment from taking place (if the error is to occur). In this division example, you have to write code that will prevent division by zero from taking place, and possibly informing the user of the mistake he made by inputting zero as a denominator.

    I have prepared a tutorial series, which shows you how to handle errors, the OOP way in PHP. The series has been written in a step-by-step fashion. The code samples are well formatted with good indentation, making readability very easy. There are no missing special characters, as you would find in other sites. The links to the different parts of the series are easily accessible. Click the following link to start the series:

    Tutorials for Exception Handling in PHP