当前位置:首页 > PHP教程 > PHP高级教程

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

本文实例讲述了php实现的简单异常处理类。分享给大家供大家参考,具体如下:

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

【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!