Chaitanya Lakshmi
  • About Me
  • Job Duties
  • Knowledge Blog
  • Applications
  • Contact Us
    • Linked In
    • Facebook
  • About Me
  • Job Duties
  • Knowledge Blog
  • Applications
  • Contact Us
    • Linked In
    • Facebook

How to create Custom Exception Class?

2/24/2012

0 Comments

 
<?php
/* Creating Custom Exception Class */
class customException extends Exception {
    public function errorMessage() {
        //error message
        $errorMsg = 'Error on line '.$this->getLine().' in '.$this->getFile()
        .': <b>'.$this->getMessage().'</b> is not a valid E-Mail address';
        return $errorMsg;
    }
}
/* Creating Custom Exception Class */


$email = "wrongEmail@domain...com";
try {
    //check if
    if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {
        //throw exception if email is not valid
        throw new customException($email);
    }
    echo "Email Id is Valid";
} catch (customException $e) {
  //display custom message
  echo $e->errorMessage();
}
?>
0 Comments

What is an Exception Handling?

2/24/2012

0 Comments

 
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.

This is what normally happens when an exception is triggered:
  • 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
We will show different error handling methods:
  • Basic use of Exceptions
  • Creating a custom exception handler
  • Multiple exceptions
  • Re-throwing an exception
  • Setting a top level exception handler
Note: 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.

0 Comments

Example how to handle an Exception in PHP?

2/24/2012

0 Comments

 
<?php
/* Function to Throw an Exception */
function checkNum($number) {
    if($number>1) {
        throw new Exception("Value must be 1 or below");
    }
    return true;
}
/* Function to Throw an Exception */

//trigger exception in a "try" block
try {
    checkNum(2);
    //If the exception is thrown, this text will not be shown
    echo 'If you see this, the number is 1 or below';
} catch(Exception $e) {
    echo 'Message: ' .$e->getMessage();
}
?>
0 Comments

What are Try , Throw and Catch blocks in PHP?

2/24/2012

1 Comment

 
  1. Try - A function using an exception should be in a "try" block. If the exception does not trigger, the code will continue as normal. However if the exception triggers, an exception is "thrown"
  2. Throw - This is how you trigger an exception. Each "throw" must have at least one "catch"
  3. Catch - A "catch" block retrieves an exception and creates an object containing the exception information
1 Comment

    Archives

    April 2012
    March 2012
    February 2012

    Categor

    All
    Ajax
    Api
    Cross Site Scripting
    Css
    Curl
    Design Patterns
    Drupal
    Exception Handling
    Htaccess
    Html
    Javascript
    Jquery
    Json
    Linux
    Mysql
    Oops
    Php
    Regular Expressions
    Web 2.0
    Webservices
    Wordpress
    Xhtml

    RSS Feed

Powered by Create your own unique website with customizable templates.