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

yipeiwu_com5年前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程序设计有所帮助。

相关文章

PHP读取txt文件的内容并赋值给数组的代码

2010-12-15.txt的文件内容如下: 复制代码 代码如下: 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20...

PHP 使用header函数设置HTTP头的示例解析 表头

如下所示:复制代码 代码如下://定义编码  header( 'Content-Type:text/html;charset=utf-8 ');  //Atom&nb...

PHP抽象类与接口的区别详解

对于面向对象开发,抽象类与接口这两个东西是比较难理解的;就算是对于有一定经验的程序员来说也如此。下面根据自己的理解来讲述一下这两个东西,如有什么不对的,还望不吝赐教。 抽象类:是基于类来...

php开发环境配置记录

【apache安装】 复制代码 代码如下: httpd.exe -k install -n "apache2.2.15" httpd.exe -k start -n "apache2.2...

php截取视频指定帧为图片

截取视频指定帧为图片,php ffmpeg扩展已经完美实现: $movie = new ffmpeg_movie($video_filePath); $ff_frame = $mov...