PHP Exception Handling

Posted by tutor | Posted in PHP Tutorial | Posted on 17-12-2009-05-2008

0

Exceptions are special conditions which occur during execution and change the normal flow of a code.

These exceptions need to be handled to let the program going on.

PHP 5 provides an object oriented way of dealing with errors.

During the execution of a program some condition may appear when the program flow is need to be changed to protect the code from crashing or producing wrong results or stopping in the middle of execution. This is called Exception handling. And exceptions are the condtions which may cause any of the above situations.

When an exception occurs the following steps are executed:

  • The current code state is saved
  • The code execution will switch to a predefined (custom) exception handler function
  • Depending on the situation, the handler may then resume the execution from the saved code state, terminate the script execution or continue the script from a different location in the code

Basic Idea for an Exception to handle is:

  • decide whether the Error produced needs to be handled and how
  • which exception to handle at which level and which point of tim
  • your code should keep running even if a severe (non-fatal) error occurs
  • make sure all errors of all the different types are handled uniformly.

Exceptions should only be used with error conditions, and should not be used to jump to another place in the code at a specified point.

When an exception is thrown, the execution of the following code block is stopped, and PHP will try to find the code for catching the exception.

If an exception is not caught, a fatal error will be issued with an “Uncaught Exception” message.

PHP 5 handles exceptions in a similar way to that of other programming languages.

Exception handling introduces some new keywords as:

  • Try - It encloses the block of code or the function which is doubted to produce an exception. If the exception does not trigger, the code will continue as normal. However if the exception triggers, an exception is thrown automatically.
  • Throw – Throw lets you trigger an error manually.
  • Catch – It contains the code to handle the exceptions. A “catch” block retrieves an exception and creates an object containing the exception information.

When an exception is thrown, the program execution will be paused to handle the error. PHP will then attempt to find the first matching catch block. If an exception is not caught, a PHP Fatal Error will be issued with an “Uncaught Exception”

  • Each try must have at least one corresponding catch block.
  • Multiple catch blocks can be used to catch different classes of exceptions.
  • Exceptions can be thrown (or re-thrown) within a catch block also.

Lets try to throw an exception without catching it:

    <?php         //create function with an exception
    function checkNum($number)
    {
    if($number==0)
    {
    throw new Exception(“Value must not be 0″);
    }
    return true;
    }
    //trigger exception
    checkNum(0);
    ?>
    Output:

    Fatal error: Uncaught exception ‘Exception’
    with message ‘Value must not be 0′ in C:\wapplications\PHPprog.php:6
    Stack trace: #0 C:\wapplications\PHPprog.php(12):
    checkNum(28) #1 {main} thrown in C:\wapplications\PHPprog.php on line

Write a comment

Twitter Users
Enter your personal information in the form or sign in with your Twitter account by clicking the button below.