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

PHP实现的简单异常处理类示例

时间:2017-05-12 17:27:33      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:exception   实现   set   return   wan   lin   nbsp   php   har   

 

具体如下:

<?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;
}

 

北京网站建设

PHP实现的简单异常处理类示例

标签:exception   实现   set   return   wan   lin   nbsp   php   har   

原文地址:http://www.cnblogs.com/1584779745qq/p/6846202.html

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