<?php /**************************************************** * php处理异常 * try中不主动throw,会先出现PHP的系统错误 ****************************************************/ header("content-type:test/html:charset=utf-8"); error_reporting(-1); try { $num1 = 3; $num2 = 0; if ($num2 == 0) { throw new Exception("自定义错误"); } else { $res = $num1 / $num2; } } catch (Exception $e) { echo $e->getMessage(); // die(); // 终止异常 } /****************************************************** * php+mysql+pdo *****************************************************/ try { $pdo = new PDO("mysql:host=localhost;dbname=mysql", "root", ""); } catch (PDOException $e) { echo $e->getMessage(); // die(); // 终止异常 } /****************************************************** * php+文件异常 *****************************************************/ /** * PHP 读取大文件 SplFileObject : * https://blog.csdn.net/ekliu/article/details/8855907 */ // SqlFileObject相对于传统的open($filename, 'r')产生的对象的优点在于不需要打开文件句柄 不需要关闭句柄更加的方便 $handle = new SplFileObject("sid_list.txt"); while (!$handle->eof()) { $item = $handle->fgets(); } try { $pdo = new SplFileObject("text.txt", "r"); echo "read File"; } catch (Exception $e) { echo $e->getMessage(); // die(); // 终止异常 } /****************************************************** * php异常 嵌套 *****************************************************/ try { throw new Exception("测试异常1"); } catch (Exception $e) { echo $e->getMessage(); // die(); // 终止异常 try { throw new Exception("测试异常2"); } catch (Exception $e) { echo $e->getMessage(); } } /****************************************************** * php异常 自定义异常封装 *****************************************************/ class MyException extends Exception { public function __construct($message = "", $code = 0, $previous = null) { parent::__construct($message, $code, $previous); } public function __toString() { $message = "<h2>出现异常,如下:</h2>"; $message .= "<p>" . __CLASS__ . "[{$this->code}:{$this->message}]</p>"; return $message; } /****************自定义异常方法***************/ public function test() { echo "这是自定义错误"; } public function stop() { exit("异常 end..."); } } // 开始调用 MyException try { echo "出现异常啦"; throw new MyException("测试自定义异常", 3); } catch (MyException $e) { echo $e->getMessage(); } // 嵌套使用 MyException 与 Exception (没有顺序) try { throw new MyException("测试自定义异常"); } catch (Exception $e) { echo $e->getMessage(); } catch (MyException $e) { echo $e->getMessage(); } /****************************************************** * php异常 自定义异常封装 文件 *****************************************************/ class FileException extends Exception { public function getDetails() { switch ($this->code) { case 0: return "没有提供文件"; break; case 1: return "文件不存在"; break; case 2: return "不是一个文件"; break; case 3: return "文件不可写"; break; case 4: return "非法文件的操作模式"; break; } } } class WriteData { private $_message = ""; private $_fp = null; public function __construct($filename = null, $mode = "w") { $this->_message = "文件:{$filename} ; 模式:{$mode}"; if (empty($filename)) throw new FileException($this->_message, 0); if (!file_exists($filename)) throw new FileException($this->_message, 1); if (!is_file($filename)) throw new FileException($this->_message, 2); // is_writable — 判断给定的文件名是否可写 if (!is_writable($filename)) throw new FileException($this->_message, 3); if (!in_array($mode, array("w", "w+", "a", "a +"))) throw new FileException($this->_message, 4); $this->_fp = fopen($filename, $mode); } public function write($data) { if (@!fwrite($this->_fp, $data . PHP_EOL)) throw new FileException($this->_message, 5); } public function close() { if ($this->_fp) { if (!fclose($this->_fp)) throw new FileException($this->_message, 6); $this->_fp = null; } } public function __destruct() { $this->close(); } }123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
PHP异常(try catch)
最新推荐文章于 2023-03-31 09:52:28 发布
本文详细介绍了PHP中的异常处理,包括try-catch块的使用,如何抛出和捕获异常,以及异常类的结构。通过实例代码,阐述了如何在程序中有效地利用异常来提高错误处理的效率和代码的可读性。
摘要由CSDN通过智能技术生成