Learning PHP-错误和异常处理

[b]Try...Catch[/b]

<?php
try{
}
catch(Exception $e){
}
?>

[b]Exception类[/b]
PHP为异常处理提供了内之类——Exception。
除了构造函数外,该类还提供了如下所示的内置方法:
[list]
[*]getCode() 返回传递给构造函数的代码。
[*]getMessage() 返回传递给构造函数的消息
[*]getFile() 返回产生异常的代码文件的完整路径
[*]getLine() 返回代码文件中产生异常的代码行号
[*]getTrace() 返回一个包含了产生异常的代码回退路径的数组
[*]getTraceAsString() 返回与getTrace()方向相同的信息,该信息将被格式化成一个字符串。
[*]_ToString() 允许简单地显示一个Exception对象,并且给出以上所有方法可以提供的信息。
[/list]
[b]用户自定义异常[/b]
<?php
class Exception{

function __construct(string $message = null, int $code = 0){
if(func_num_args()){
$this->message = $message;
}
$this->code = $code;
$this->file = __FILE__;
$this->line = __LINE__;
$this->trace = debug_backtrace();
$this->string = StringFormat($this);
}

protected $message;
protected $code = 0;
protected $file;
protected $line;

private $trace;
private $string;

final function getMessage(){
return $this->message;
}

final function getCode(){
return $this->code;
}

final function getTrace(){
return $this->trace;
}

final function getTraceAsString(){
return self::TraceFormat($this);
}

function _toString(){
return $this->string;
}

static private function StringFormat(Exception $exception){
}

static private function TraceFormat(Exception $exception){
}
}
?>


参考资料:
PHP&MySQL.Web
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值