您的位置:首页 > 编程语言 > PHP开发

php异常处理类

2016-05-25 23:18 323 查看
<?php
header('content-type:text/html;charset=UTF-8');

// 创建email异常处理类
class emailException extends exception
{
}
// 创建pwd异常处理类
class pwdException extends exception
{
public function __tostring(){
return $this->getMessage().'in file:'.$this->getFile().'on line:'.$this->getLine();
}
}

function reg($reginfo = null)
{
// 依据不同错误抛出不同异常
if (empty($reginfo) || !isset($reginfo)) {
throw new Exception('参数非法');
}
if (empty($reginfo['email'])) {
throw new emailException('邮件为空');
}
if ($reginfo['pwd'] != $reginfo['repwd']) {
throw new pwdException('两次密码不一致!');
}
}

// 接收不同异常,并针对性处理!
try {
reg(array('email' => '1078789950@qq.com', 'pwd' => '123', 'repwd' => '1231' ));
} catch (Exception $e) {
echo $e ->getMessage();
} catch (emailException $ee) {
echo $ee ->getMessage();
} catch (pwdException $ep) {
echo $ep;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: