PHP 错误与异常 笔记与总结(13 )自定义异常类

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

 

自定义异常类

 1 <?php
 2 header('content-type:text/html; charset=utf-8');
 3 /*
 4     自定义异常类
 5 */
 6     class MyException extends Exception{
 7         //重写父类构造函数
 8         public function __contruct($message, $code = 0){
 9             parent::__contruct($message, $code);
10         }
11 
12         //重写 __toString 方法
13         public function __toString(){
14             $message = '<h2>出现异常,信息如下:</h2>';
15             $message .= '<p>'.__CLASS__.' ['.$this->code.']:'.$this->message.'</p>';
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 '<hr>';
36 
37         //__toString
38         echo $e;    
39         echo '<hr>';
40 
41         //自定义方法test()
42         echo $e->test();    
43         echo '<hr>';
44 
45         //自定义方法stop()
46         echo $e->stop();    
47         echo '<hr>';
48     }
49 
50     echo 'continue';

输出:

 

 

多个 catch 进行捕获

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

 1 <?php
 2 header('content-type:text/html; charset=utf-8');
 3 /*
 4     自定义异常类
 5 */
 6     class MyException extends Exception{
 7         //重写父类构造函数
 8         public function __contruct($message, $code = 0){
 9             parent::__contruct($message, $code);
10         }
11 
12         //重写 __toString 方法
13         public function __toString(){
14             $message = '<h2>出现异常,信息如下:</h2>';
15             $message .= '<p>'.__CLASS__.' ['.$this->code.']:'.$this->message.'</p>';
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 '<hr>';
36 
37         //调用自定义方法stop()
38         $e->stop();
39         echo '<hr>';
40     }catch(Exception $e){
41         echo $e->getMessage();
42     }
43 
44     echo '<hr>';
45     echo 'continue';

 

输出:

 

 

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

① 测试 “文件不存在”:

WriteToFile.php

 1 <?php
 2 header('content-type:text/html; charset=utf-8');
 3 /*
 4     自定义文件写入异常类
 5 */
 6 class FileException extends Exception{
 7     //自定义方法
 8     public function getDetails(){
 9         $code = '错误码['.$this->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}<br>模式:{$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 '数据写入成功<hr />';
79 }catch(FileException $e){
80     echo '<h2>出现异常</h2>',$e->getMessage(),'<br>信息详情:',$e->getDetails();
81 }

执行结果:

 

 

② 测试 “文件不存在”

把 line :75 的

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

改为:

$fp = new WriteData();

执行结果:

 

 

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

 

文件 test.txt:

 

转载于:https://www.cnblogs.com/dee0912/p/4621204.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值