欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-12 21:20:08
本文实例讲述了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;
}

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

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