博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PHP 错误与异常 笔记与总结(13 )自定义异常类
阅读量:6933 次
发布时间:2019-06-27

本文共 4133 字,大约阅读时间需要 13 分钟。

针对不同的异常,进行不同的处理,可以通过自定义异常类记录特定的异常信息来处理不同类型的异常。自定义异常类通过继承基类(Exception),对基类进行扩展。

 

自定义异常类

1 
出现异常,信息如下:';15 $message .= '

'.__CLASS__.' ['.$this->code.']:'.$this->message.'

';16 return $message;17 }18 19 //自定义方法20 public function test(){21 echo 'test.';22 }23 24 public function stop(){25 exit('script end....');26 }27 }28 29 try{30 echo '出现异常...';31 throw new MyException('测试自定义异常', 3);32 }catch(MyException $e){33 //输出:测试自定义异常34 echo $e->getMessage(); 35 echo '

';36 37 //__toString38 echo $e; 39 echo '

';40 41 //自定义方法test()42 echo $e->test(); 43 echo '

';44 45 //自定义方法stop()46 echo $e->stop(); 47 echo '

';48 }49 50 echo 'continue';

输出:

 

 

多个 catch 进行捕获

当程序捕获一个个异常时,就不会再进行后面的 catch 分支了,而是直接执行 try{}catch(){} 以后的代码。在捕获多个异常时,应该把基类 Exception 放到最后捕获。例:

1 
出现异常,信息如下:';15 $message .= '

'.__CLASS__.' ['.$this->code.']:'.$this->message.'

';16 return $message;17 }18 19 //自定义方法20 public function test(){21 echo 'test.';22 }23 24 public function stop(){25 exit('script end....');26 }27 }28 29 try{30 throw new MyException('测试自定义异常');31 }catch(MyException $e){32 echo $e->getMessage();33 //调用自定义方法test()34 $e->test();35 echo '

';36 37 //调用自定义方法stop()38 $e->stop();39 echo '

';40 }catch(Exception $e){41 echo $e->getMessage();42 }43 44 echo '

';45 echo 'continue';

 

输出:

 

 

【例】自定义 文件写入异常处理类

① 测试 “文件不存在”:

WriteToFile.php

1 
code.'] ';10 switch($this->code){11 case 0:12 return $code.'没有提供文件';13 break;14 case 1:15 return $code.'文件不存在';16 break;17 case 2:18 return $code.'不是一个文件';19 break;20 case 3:21 return $code.'文件不可写';22 break;23 case 4:24 return $code.'非法的文件操作模式';25 break;26 case 5:27 return $code.'文件写入失败';28 break;29 case 6:30 return $code.'文件不能被关闭';31 break;32 default:33 return $code.'非法';34 break;35 }36 }37 }38 39 /*40 写入文件的类41 */42 class WriteData{43 private $_message = '';44 private $_fp = null; //文件句柄45 public function __construct($filename = null, $mode = 'w'){46 $this->_message = "文件:{
$filename}
模式:{
$mode}";47 if(empty($filename)) throw new FileException($this->_message, 0);48 if(!file_exists($filename)) throw new FileException($this->_message, 1);49 if(!is_file($filename)) throw new FileException($this->_message, 2);50 if(!is_writeable($filename)) throw new FileException($this->_message, 3);51 if(!in_array($mode, array('w', 'w+', 'a', 'a+'))) throw new FileException($this->_message, 4);52 $this->_fp = fopen($filename, $mode);53 }54 //写文件55 public function write($data){56 if(@!fwrite($this->_fp, $data.PHP_EOL)) throw new FileException($this->_message.'数据写入失败', 5);57 }58 59 //关闭资源句柄60 public function close(){61 if($this->_fp){62 if(@!fclose($this->_fp)) throw new FileException($this->_message.'文件关闭失败', 6);63 $this->_fp = null; 64 }65 }66 67 //析构方法68 public function __destruct(){69 $this->close();70 }71 }72 73 //测试74 try{75 $fp = new WriteData('test.txt', 'w');76 $fp->write('测试数据');77 $fp->close();78 echo '数据写入成功

';79 }catch(FileException $e){80 echo '

出现异常

',$e->getMessage(),'
信息详情:',$e->getDetails();81 }

执行结果:

 

 

② 测试 “文件不存在”

把 line :75 的

$fp = new WriteData('test.txt', 'w');

改为:

$fp = new WriteData();

执行结果:

 

 

③ 在当前目录新建文件 test.txt,执行 php 文件,输出:

 

文件 test.txt:

 

转载地址:http://upgjl.baihongyu.com/

你可能感兴趣的文章
OO学习之二——面向对象分析(OOD)的介绍
查看>>
深入python3 (Dive Into Python 3) 在线阅读与下载
查看>>
linux 更改服务的启动顺序
查看>>
【数据结构】除去线性表中的重复数字
查看>>
[原]IE9 DOM的自定义属性问题
查看>>
[CLR via C#]17. 委托
查看>>
Android系统Google Maps开发实例浅析
查看>>
支持向量机(SVM)算法
查看>>
445port入侵具体解释
查看>>
The command 'new_value' for SQLPlus
查看>>
【转】弧度和角度的转换
查看>>
Servlet 第六课: Session的使用
查看>>
Ubuntu14.04安装JDK
查看>>
虚拟机克隆以后出现“需要整合虚拟机磁盘”的解决方法
查看>>
InstallShield12豪华版破解版下载|InstallShield下载|软件打包工具
查看>>
web项目构建
查看>>
MVC中,视图的Layout使用
查看>>
Samba通过ad域进行认证并限制空间大小《转载》
查看>>
理解事件捕获。在限制范围内拖拽div+吸附+事件捕获
查看>>
[Android] 基于 Linux 命令行构建 Android 应用(六):Android 应用签名
查看>>