`
zhengdl126
  • 浏览: 2509707 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类

try{}catch{}

    博客分类:
  • php
 
阅读更多

<?php


/*

检测(try)、抛出(throw)和捕获(catch)异常。一个 try 至少要有一个与之对应的 catch。定义多个 catch 可以捕获不同的对象。
PHP 会按这些 catch 被定义的顺序执行,直到完成最后一个为止。而在这些 catch 内,又可以抛出新的异常。
*/

 

 




/*
try {
    $error = 'Always throw this error';
    throw new Exception($error);

    // 从这里开始,try 代码块内的代码将不会被执行
    echo 'Never executed';

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

// 继续执行
echo 'Hello World';
*/





/*
try {
    echo 1/0;
} catch(Exception $e) {
    echo $e->getMessage();
}


*/

 

 

 

/*

 

 try{
                    $filter = ClassLoader::getInstance($obj_filter);
                    self::$_cacheStore->put($cacheKeyName, $filter);
                }catch(SystemException $e){
                    $e->message = $e->getMessage() . '[in'.__FILE__.', line:'.__LINE__.' ]';
                    throw $e;
                }

*/

 

 

 

======================================

 



class SystemException extends Exception{

    const PHP_ERROR = '100';
   
    const FRAMEWORK_ERROR = '200';
   
    const CACHE_ERROR = '300';
   
    const DB_ERROR = '400';
   
    const DAO_ERROR = '500';

    public $type;
    public $message;
   
    public $id; 

    public function SystemException($type,$message="",$id=1000) {
        $this->type = $type;
        $this->message = $message;
        $this->id = $id;
    }
}





    function get_my_uri($encode = 0, $cache = 1){
        static $retval = false;
        if ($cache && $retval !== false)
        return $retval;
        @$protocol = $_SERVER['HTTPS'] == 'on' ? 'https' : 'http';
        $port = $_SERVER['SERVER_PORT'];
        if ($protocol == 'http' && $port == '80')
        $port = '';
        else if ($protocol == 'https' && $port == '443')
        $port = '';
        else
        $port = ":$port";
        @$http_host  = $_SERVER['SERVER_NAME'];
        $request_uri = $_SERVER['REQUEST_URI'];
        $retval = "$protocol://$http_host$port$request_uri";
        if ($encode)
        $retval = urlencode($retval);
        return $retval;
    }

try{
    $error = 'Always throw this error';
   throw new SystemException($error);
   echo 'Never executed';
}catch(SystemException $e){
    $msg = "Error request url:" . get_my_uri();
    $msg .= "\nSystemException";
    $msg .= "\nID:". $e->id;
    $msg .= "\nTYPE:". $e->type;
    $msg .= "\nMESSAGE:" . $e->message;
    $msg .= "\n";
    echo $msg;
    // log_error($msg);
    //header_to('error.htm')

}


?>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics