本文实例讲述了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;
}

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP错误与异常处理方法总结》、《php字符串(string)用法总结》、《PHP数组(Array)操作技巧大全》、《PHP运算与运算符用法总结》、《PHP网络编程技巧总结》、《PHP基本语法入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

点赞(57)

评论列表共有 0 条评论

立即
投稿
返回
顶部