Create your own exception class in PHP

Description

The following code shows how to create your own exception class.

Example


<?php/*from  w ww. j  av  a2 s .  com*/

class MyException extends Exception { 
    function __construct($language,$errorcode) { 
        $this->language = $language;
        $this->errorcode = $errorcode;
    } 

    function getMessageMap() {
        $errors = file("errors/".$this->language.".txt");
        foreach($errors as $error) {
             list($key,$value) = explode(",",$error,2);
             $errorArray[$key] = $value;
        }
        return $errorArray[$this->errorcode];
    }  
} # end MyException

try {
    throw new MyException("english",4);
}
catch (MyException $e) {
    echo $e->getMessageMap();
}

?>




















Home »
  PHP Tutorial »
    Language Basic »




PHP Introduction
PHP Operators
PHP Statements
Variable
PHP Function Create
Exception
PHP Class Definition