码迷,mamicode.com
首页 > Web开发 > 详细

Php 异常处理 exception

时间:2015-05-05 12:25:13      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:

<?php

$a = null;

//没有接收一场处理
if($a == 0) {
        throw new DbException(‘val is empty‘);
    }
    
    exit();
    
// 接收异常并处理    
try {
    if($a == 0) {
        throw new DbException(‘val is empty‘);
    }
} catch (DbException $e) {
    echo $e->showMessage(); //输出异常信息。 
} 

// 记录异常,定义不同类型异常处理
class DbException extends Exception{
    
    public function __construct ($message = ‘参数异常‘, $code = 0) {
        parent::__construct($message, $code);
    }
    
    public function showMessage() {
        $msg = $this->getMessage();
        file_put_contents(‘./info.txt‘, 
                ‘Exception:‘ . $msg . "\n file:" .  $this->getFile() . "\n line:" . $this->getLine() . "\n Trac:" . var_export($this->getTrace(), true), 
                FILE_APPEND);
    }
}

 

Php 异常处理 exception

标签:

原文地址:http://www.cnblogs.com/bandbandme/p/4478666.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!