Create custom exception handler


Exception handling is used to change the normal flow of the code execution if a specified error (exceptional) condition occurs. This condition is called an exception.

PHP has an exception model similar to that of other programming languages. Exceptions are important and provides a better control over error handling.


There are following functions which can be used from Exception class.
  • getMessage()- message of exception
  • getCode() - code of exception
  • getFile() - source filename
  • getLine() - source line
  • getTrace() - n array of the backtrace()
  • getTraceAsString() - formated string of trace


<?php
try {
    $error = 'Always throw this error';
    throw new Exception($error);

    // Code following an exception is not executed.
    echo 'Never executed';

} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

// Continue execution
echo 'Hello World';
?>

In the above example $e->getMessage function is uded to get error 
message. 


All errors and warnings should be logged. Based on the severity of an error, notifications should be sent out to other systems/teams. So that you can better gauge its severity, PHP provides several built-in error levels to describe the nature of an error. Each level is represented by an integer value and named constant which can be used by the programmer.  

The following table is generated from http://php.net/manual/en/errorfunc.constants.php





You can extend PHP's built in Exception class and create custom Exception handler for more flexibility.

Here I have created a custom exception handler

<?php
error_reporting(E_ALL);
// error_reporting(E_ALL | E_STRICT);  // From PHP 5.4.0 E_STRICT is a part of E_ALL

class NoticeException extends Exception { 
    public function __toString() {
        return  "NOTICE :: $this->message";
    }
}

class WarningException extends Exception { 
    public function __toString() {
        return  "WARNING :: $this->message";
    }
}

class StrictException extends Exception {
    public function __toString() {
        return  "STRICT :: $this->message";
    }
}

class UserThrownException extends Exception {
    public function __toString() {
        return "USER ERROR :: $this->message";
    }
}

class UserDepricatedException extends Exception {
    public function __toString() {
        return "USER DEPRICATED ERROR :: $this->message";
    }
}

class OtherErrorException extends Exception {
    public function __toString() {
        return  "OTHER ERROR :: $this->message";
    }
}

function pc_error_handler($errno,$error,$file,$line,$context) {
    $message = "[$error][FILE : $file][LINE : $line]";
    
    if($errno == E_WARNING) {
        throw new WarningException($message);
    } else if($errno == E_NOTICE) {
        throw new NoticeException($message);
    } else if($errno == E_STRICT) {
        throw new StrictException($message);
    } else if($errno == E_USER_ERROR) {
        throw new UserThrownException($message);
    } else if($errno == E_USER_DEPRECATED) {
        throw new UserDepricatedException($message);
    } else {
        throw new OtherErrorException($message);
    }
}

13 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Absolutely fabulous! Thanks for giving out excellent information. Your post is fascinating and I have forwarded it to some of my links.
    online college papers

    ReplyDelete
  3. Great article! This is the type of information that are meant to be public around the internet.
    college paper writing services

    ReplyDelete
  4. Really appreciate the Collective Impact concepts. I'm impressed by the details that you have on this web site. This is really very beneficial site. write my college essay

    ReplyDelete
  5. I wondered upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I’ll be subscribing to your feed and I hope you post again soon.
    iOS Training in Chennai
    Android Training in Chennai
    php Training in Chennai

    ReplyDelete
  6. I just keep visiting this blog from past two years. The posts here are unique and useful than other blog posts. Such a great info provided in this post. Keep providing useful post in this blog .I appreciate author for this valuable post.
    Culinary Schools in Dubai | Dubai Cooking Schools | Sugar craft courses | ICCA

    ReplyDelete
  7. Great publish, very useful information. I really appreciate this post. You must continue your writing. I'm confident, you've a huge readers' base already!
    Engineering Colleges in Chennai, ECE Engineering Colleges in Chennai

    ReplyDelete
  8. Somebody necessarily help to make severely posts I might state. This is the first time I frequented your website page and to this point? I surprised with the research you made to create this particular post extraordinary. Well done admin..
    PHP Training in Chennai

    ReplyDelete
  9. I wondered upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I’ll be subscribing to your

    feed and I hope you post again soon.

    Android App Development Company

    ReplyDelete
  10. I wondered upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I’ll be subscribing to your feed and I hope you post again soon.
    iOS App Development Company
    iOS App Development Company

    ReplyDelete
  11. Being new to the blogging world I feel like there is still so much to learn. Your tips helped to clarify a few things for me as well as giving..
    Fitness SMS
    Fitness Text
    Salon SMS
    Salon Text
    Investor Relation SMS
    Investor Relation Text

    ReplyDelete
  12. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
    Android Training in Chennai
    Ios Training in Chennai

    ReplyDelete