Hire a web Developer and Designer to upgrade and boost your online presence with cutting edge Technologies

Sunday, June 30, 2013

PHP Exception Explained

In this long (I think so) article, we will see about basics of PHP Exception, how to work with it, advantages, disadvantage and how we can extend the Exception with our custom class.

What is an Exception?

An Exception is an abnormal (Unwanted) conditions which arise at the time of execution of Program.I also say exception is “Run-Time Error”. Concept of exception was introduced in PHP 5 with OOP. Exception interrupt the program flow if something goes wrong in it.(exception e). e is an object of class.An exception is an object that is thrown by program.

Main use of Exception

With the use of exception you will have better control over your errors and bugs. You can show custom messages with the use of Exception.

Exception in PHP

PHP has Exception handler function which can handle your exception once there is any exception triggered otherwise program will stops executing.

Exception Blocks in PHP

There are 3 predefined words in PHP for Exception
  1. Try-catch
  2. Throw
  3. Try-Finally

try
{
    throw new Exception($e);
}
catch
{
    // code will accept exception if its thrown
}
Let’s start with try-catch block for handling exception

Try-Catch

Try is a block of code to caught error condition and catch is the block of code which catches the exception thrown by try block and also handle it. try can be used with multiple catch block but catch can’t be useful with multiple try block.Its only have one try block.
Catch statement works like method definition. Its passed single parameter which reference to the exception object.

    Catch(Exception e)
If parameter matches to the exception which was generated then exception caught otherwise exception uncaught or bypassed.
try without catch or finally is invalid.

Throw

As the name Throw is used to throw the custom Exception. Custom Exception is useful to throw the custom error based on application logic. There must be an argument with throw statement. This argument is generally a Error Description.

Try – Finally

Simply say finally block should execute in all cases. Finally is a block of code that will be execute ,if exception generated or not. Its a code block which gives guarantee to be executed once when your program run.
PHP does not support finally. (Reference)

Example of the Basic Exception


Class MyExceptions
{
  function errorDisplay()
  {
    try
    {
     $x= 0;
     if($x != 0)
     {
      $y = 50/$x;
     }
     else
     {
      throw new Exception('Divison By Zero');
     }
    }
    catch (Exception $e)
    {
      var_dump($e->getMessage());
    }

  }
}

  $obj = new MyExceptions();

  $obj->errorDisplay();
Here in above example i have explained about try and catch with custom exception. I have created one class MyExceptions which contain method errorDisplay() and in this method i have taken two numbers and divide it by zero. And its normal error mostly happened when we try to apply division so its generate warning but we can caught error by exception using throw.
Let’s move to the its advantages/disadvantages

Advantages

  1. You can fix your error by custom message.
  2. Its prevent program from automatically terminating.

Drawbacks

  1. Exception cannot be work with php 4.

Extending the Exception Class

With the use of object we can extend exception class and its useful to return some information and some kinds of exceptions. we can extend Exception class with other class by extends keyword.To extend exception is also good if we need to use abstract class.

class MyException extends Exception
{

  function Checknumber($no,$n)
  {
    if($no== $n)
    {
      throw new MyException('Both numbers should not same');
    }
    else
      echo 'Numbers are '.$no.' and '.$n ;
  }

}

try
{
  $obj = new MyException();
  $obj->Checknumber(2,2);

}
catch(MyException $e)
{
  echo $e->getMessage();
}
You can’t use try on single statement. It must be surrounded by { } parenthesis.
Here i have used MyException class to extend Exception class and in class function that accepts two numbers and check the values and here i have added condition that throws exception if both numbers are equal. otherwise its execute program normally.
Well, that’s it from my end on PHP Exception. Feel free to comment below if you have any comments/question/suggestion.

No comments:

Post a Comment