当前位置:Gxlcms > PHP教程 > PHP出现异常该怎么办

PHP出现异常该怎么办

时间:2021-07-01 10:21:17 帮助过:17人阅读

本篇文章给大家介绍一下解决PHP出现异常的方法。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。

在这里插入图片描述

  1. <?php
  2. /****************************************************
  3. * php处理异常
  4. * try中不主动throw,会先出现PHP的系统错误
  5. ****************************************************/
  6. header("content-type:test/html:charset=utf-8");
  7. error_reporting(-1);
  8. try {
  9. $num1 = 3;
  10. $num2 = 0;
  11. if ($num2 == 0) {
  12. throw new Exception("自定义错误");
  13. } else {
  14. $res = $num1 / $num2;
  15. }
  16. } catch (Exception $e) {
  17. echo $e->getMessage();
  18. // die(); // 终止异常
  19. }
  20. /******************************************************
  21. * php+mysql+pdo
  22. *****************************************************/
  23. try {
  24. $pdo = new PDO("mysql:host=localhost;dbname=mysql", "root", "");
  25. } catch (PDOException $e) {
  26. echo $e->getMessage();
  27. // die(); // 终止异常
  28. }
  29. /******************************************************
  30. * php+文件异常
  31. *****************************************************/
  32. /**
  33. * PHP 读取大文件 SplFileObject :
  34. * https://blog.csdn.net/ekliu/article/details/8855907
  35. */
  36. // SqlFileObject相对于传统的open($filename, 'r')产生的对象的优点在于不需要打开文件句柄 不需要关闭句柄更加的方便
  37. $handle = new SplFileObject("sid_list.txt");
  38. while (!$handle->eof()) {
  39. $item = $handle->fgets();
  40. }
  41. try {
  42. $pdo = new SplFileObject("text.txt", "r");
  43. echo "read File";
  44. } catch (Exception $e) {
  45. echo $e->getMessage();
  46. // die(); // 终止异常
  47. }
  48. /******************************************************
  49. * php异常 嵌套
  50. *****************************************************/
  51. try {
  52. throw new Exception("测试异常1");
  53. } catch (Exception $e) {
  54. echo $e->getMessage();
  55. // die(); // 终止异常
  56. try {
  57. throw new Exception("测试异常2");
  58. } catch (Exception $e) {
  59. echo $e->getMessage();
  60. }
  61. }
  62. /******************************************************
  63. * php异常 自定义异常封装
  64. *****************************************************/
  65. class MyException extends Exception
  66. {
  67. public function __construct($message = "", $code = 0, $previous = null)
  68. {
  69. parent::__construct($message, $code, $previous);
  70. }
  71. public function __toString()
  72. {
  73. $message = "<h2>出现异常,如下:</h2>";
  74. $message .= "<p>" . __CLASS__ . "[{$this->code}:{$this->message}]</p>";
  75. return $message;
  76. }
  77. /****************自定义异常方法***************/
  78. public function test()
  79. {
  80. echo "这是自定义错误";
  81. }
  82. public function stop()
  83. {
  84. exit("异常 end...");
  85. }
  86. }
  87. // 开始调用 MyException
  88. try {
  89. echo "出现异常啦";
  90. throw new MyException("测试自定义异常", 3);
  91. } catch (MyException $e) {
  92. echo $e->getMessage();
  93. }
  94. // 嵌套使用 MyException 与 Exception (没有顺序)
  95. try {
  96. throw new MyException("测试自定义异常");
  97. } catch (Exception $e) {
  98. echo $e->getMessage();
  99. } catch (MyException $e) {
  100. echo $e->getMessage();
  101. }
  102. /******************************************************
  103. * php异常 自定义异常封装 文件
  104. *****************************************************/
  105. class FileException extends Exception
  106. {
  107. public function getDetails()
  108. {
  109. switch ($this->code) {
  110. case 0:
  111. return "没有提供文件";
  112. break;
  113. case 1:
  114. return "文件不存在";
  115. break;
  116. case 2:
  117. return "不是一个文件";
  118. break;
  119. case 3:
  120. return "文件不可写";
  121. break;
  122. case 4:
  123. return "非法文件的操作模式";
  124. break;
  125. }
  126. }
  127. }
  128. class WriteData
  129. {
  130. private $_message = "";
  131. private $_fp = null;
  132. public function __construct($filename = null, $mode = "w")
  133. {
  134. $this->_message = "文件:{$filename} ; 模式:{$mode}";
  135. if (empty($filename)) throw new FileException($this->_message, 0);
  136. if (!file_exists($filename)) throw new FileException($this->_message, 1);
  137. if (!is_file($filename)) throw new FileException($this->_message, 2);
  138. // is_writable — 判断给定的文件名是否可写
  139. if (!is_writable($filename)) throw new FileException($this->_message, 3);
  140. if (!in_array($mode, array("w", "w+", "a", "a
  141. +"))) throw new FileException($this->_message, 4);
  142. $this->_fp = fopen($filename, $mode);
  143. }
  144. public function write($data)
  145. {
  146. if (@!fwrite($this->_fp, $data . PHP_EOL)) throw new FileException($this->_message, 5);
  147. }
  148. public function close()
  149. {
  150. if ($this->_fp) {
  151. if (!fclose($this->_fp)) throw new FileException($this->_message, 6);
  152. $this->_fp = null;
  153. }
  154. }
  155. public function __destruct()
  156. {
  157. $this->close();
  158. }
  159. }

推荐学习:php视频教程

以上就是PHP出现异常该怎么办的详细内容,更多请关注gxlcms其它相关文章!

人气教程排行