Error Handling and Exceptions in PHP

Exception in PHP provide a unified mechanism that helps us in handling errors in an extensible,maintainable and object-oriented-way.these make it easy for the programmers to identify the path to the code where it has an error as well as they get notified on what kind of error that has occured.

Concepts of Exception Handling

In PHP exception is a code placed inside a code so that in case of an error the error is notified to the user that is “thrown”,these code is in the format.

                //php code here

The constructor for this class takes two parameters that is the code and the message,they are meant to represent an error message and an error code.just after the try block there should be at-least one catch block like this

                //handles the exception
The object passed into and (caught by) catch block is the one passed to (and thrown by) the throw statement that raised the exception.these exceptions are categorized into instance of exception class and the user-defined exceptions that inherit from exception class

An example Throwing and catching an exception

                throw new Exception("A fatal error has occurred",42);
            catch(Exception $e) {
                echo "Exception".$e->getCode().";".$e->getMessage()."<br/>"
                "in".$e->getFile()."on line " .$e->getLine(). "<br/>";

With the above code we raise exception of class Exception.this built in class has methods we can use to catch block to report a useful error message

The Exception class

PHP has inbuilt classes and it takes two parameters that is the message and error code ..these are the examples of those inbuilt methods.
  • getcode()-return the code as passed to the constructor
  • getMessage()– return the message as passed to the constructor
  • getFile()-returns the fullpath to the code file where the exception was raised
  • getTrace()-return an array containing a backtrace where the exception was raised
  • getTraceAs string()-return the same information as getTrace formated as string
  • __toString()– allows you to echo an excemption all information from the above.
User-Defined Exception
instead of instantiating  and passing an instance of the base exception class,you can pass any object you like.these am going to demonstrate using these example in a code.
an example of user-defined Exception class
       class myException extends Exception
           function __toString
            return "<table border=*1*">
            <td><strong>Exception ".$this->getCode()."
            </strong>:".$this->getMessage()." <br/>"."
            in ".$this->getFile()." on line ".$this->getLine()."
                throw new myException("A terrible error has occurred".42);
            catch (myException $m)
                echo $m;
 The above myException class provide exceptions with “pretty printing”
error and exception
Leave a Reply

Your email address will not be published.

Previous Post

How to identify the threats We Face With Web Applications

Next Post

How to implement Authentication with PHP and MySQL

Related Posts