PHP错误处理方法总结

前端之家收集整理的这篇文章主要介绍了PHP错误处理方法总结前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
PHP错误处理方法总结感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编两巴掌来看看吧!
PHP错误处理的方法有很多特别是到了PHP之后还提供了专门的PHP处理类下面我收藏了关于PHP错误处理一些方法与程序分享给大家 在程序中直接判断
基本的错误处理使用 die() 函数 第一个例子展示了一个打开文本文件的简单脚本
  1. /**
  2. * PHP错误处理方法总结
  3. *
  4. * @param
  5. * @arrange 512-笔记网: www.www.jb51.cc
  6. **/
  7. $file=fopen("welcometxt""r");
  8. /*** 来自编程之家 jb51.cc(jb51.cc) ***/

如果文件不存在您会获得类似这样的错误
Warning: fopen(welcometxt) [functionfopen]: Failed to open stream: No such file or directory in C:webfoldertestPHP on line
  1. /**
  2. * PHP错误处理方法总结
  3. *
  4. * @param
  5. * @arrange 512-笔记网: www.www.jb51.cc
  6. **/
  7. //打开一个文件 未做任何处理
  8. //$fp =fopen("aatxt""r");
  9. //echo "OK";
  10. //处理判断文件是否存在 file_exists
  11. /*
  12. if(!file_exists("aatxt")){
  13. echo "文件不存在";
  14. //不存在就退出
  15. exit(); //退出后下面面的代码就不执行了
  16. }else{
  17. $fp =fopen("aatxt""r");
  18. //操作完之后 关闭
  19. fclose($fp);
  20. }
  21. echo "OK";
  22. */
  23. //PHP处理错误的种方法
  24. //第一种使用简单的die语句
  25. /* if(!file_exists("aatxt")){
  26. die("文件不存在"); //不存在就直接退出
  27. }else{
  28. $fp =fopen("aatxt""r");
  29. //操作完之后 关闭
  30. fclose($fp);
  31. }
  32. echo "OK";
  33. */
  34. //更简单的方式
  35. file_exists("aatxt") or die("文件不存在");
  36. /*** 来自编程之家 jb51.cc(jb51.cc) ***/

第二种错误处理器 错误级别 处理错误方式
  1. /**
  2. * PHP错误处理方法总结
  3. *
  4. * @param
  5. * @arrange 512-笔记网: www.www.jb51.cc
  6. 使用error_function(error_levelerror_message
  7. error_fileerror_lineerror_context)
  8. 函数必须有能力处理至少两个参数 (error level 和 error message)
  9. 但是可以接受最多五个参数(可选的file linenumber 以及 error context)
  10. */
  11. //改写set_error_handler方法
  12. //如果出现 E_WARNING 这个错误调用my_error 处理方法
  13. set_error_handler("my_error"E_WARNING);
  14. set_error_handler("my_error"E_USER_ERROR);
  15. //设置中国对应的时区
  16. date_default_timezone_set(PRC);
  17. function my_error($errno$errmes){
  18. echo "<font size= color=red >$errno</font>"; //输出错误报告级别
  19. echo "错误信息是:"$errmes;
  20. exit();
  21. }
  22. function my_error($errno$errmes){
  23. //echo "错误信息是:"$errno$errmes;
  24. //exit();
  25. //把错误信息输入到文本中保存已备查看 使用到error_log()函数
  26. $message ="错误信息是:"$errno" "$errmes;
  27. error_log(date("Ymd G:i:s")""$message"rn""myerrortxt"); // rn 表示换行
  28. }
  29. //打开一个文件 未做任何处理
  30. //$fp =fopen("aatxt""r");
  31. //echo "OK";
  32. //使用自定义错误添加触发器 这个trigger_error()函数来指定调用自定义错误
  33. $age=;
  34. if($age>){
  35. //echo "年龄过大";
  36. //调用触发器 同时指定错误级别 这里需要查看帮助文档
  37. trigger_error("不好了出大问题了"E_USER_ERROR);
  38. //exit();
  39. }
  40. /*** 来自编程之家 jb51.cc(jb51.cc) ***/

PHP 异常处理
PHP 提供了一种新的面向对象的错误处理方法
如果异常没有被捕获而且又没用使用 set_exception_handler() 作相应的处理的话那么将发生一个严重的错误(致命错误)并且输出 "Uncaught Exception" (未捕获异常)的错误消息
让我们尝试抛出一个异常同时不去捕获它
  1. /**
  2. * PHP错误处理方法总结
  3. *
  4. * @param
  5. * @arrange 512-笔记网: www.www.jb51.cc
  6. **/
  7. //create function with an exception
  8. function checkNum($number)
  9. {
  10. if($number>)
  11. {
  12. throw new Exception("Value must be or below");
  13. }
  14. return true;
  15. }
  16. //trigger exception
  17. checkNum();
  18. /*** 来自编程之家 jb51.cc(jb51.cc) ***/
上面的代码会获得类似这样的一个错误
Fatal error: Uncaught exception Exception with message Value must be or below in C:webfoldertestPHP: Stack trace: # C:webfoldertestPHP(): checkNum() # {main} thrown in C:webfoldertestPHP on line Try throw 和 catch
要避免上面例子出现的错误我们需要创建适当的代码来处理异常
处理处理程序应当包括
Try 使用异常的函数应该位于 "try" 代码块内如果没有触发异常则代码将照常继续执行但是如果异常被触发会抛出一个异常 Throw 这里规定如何触发异常每一个 "throw" 必须对应至少一个 "catch" Catch "catch" 代码块会捕获异常并创建一个包含异常信息的对象 让我们触发一个异常
  1. /**
  2. * PHP错误处理方法总结
  3. *
  4. * @param
  5. * @arrange 512-笔记网: www.www.jb51.cc
  6. **/
  7. <?PHP
  8. //创建可抛出一个异常的函数
  9. function checkNum($number)
  10. {
  11. if($number>)
  12. {
  13. throw new Exception("Value must be or below");
  14. }
  15. return true;
  16. }
  17. //在 "try" 代码块中触发异常
  18. try
  19. {
  20. checkNum();
  21. //If the exception is thrown this text will not be shown
  22. echo If you see this the number is or below;
  23. }
  24. //捕获异常
  25. catch(Exception $e)
  26. {
  27. echo Message: $e>getMessage();
  28. }
  29. ?>
  30. /*** 来自编程之家 jb51.cc(jb51.cc) ***/
上面代码将获得类似这样一个错误
Message: Value must be or below
创建一个自定义的 Exception 类 创建自定义的异常处理程序非常简单我们简单地创建了一个专门的类当 PHP 中发生异常时可调用函数该类必须是 exception 类的一个扩展
这个自定义的 exception 类继承了 PHP 的 exception 类的所有属性您可向其添加自定义函数
我们开始创建 exception 类
  1. /**
  2. * PHP错误处理方法总结
  3. *
  4. * @param
  5. * @arrange 512-笔记网: www.www.jb51.cc
  6. **/
  7. <?PHP
  8. class customException extends Exception
  9. {
  10. public function errorMessage()
  11. {
  12. //error message
  13. $errorMsg = Error on line $this>getLine() in $this>getFile()
  14. : <b>$this>getMessage()</b> is not a valid EMail address;
  15. return $errorMsg;
  16. }
  17. }
  18. $email = "someone@exampl";
  19. try
  20. {
  21. //check if
  22. if(filter_var($email FILTER_VALIDATE_EMAIL) === FALSE)
  23. {
  24. //throw exception if email is not valid
  25. throw new customException($email);
  26. }
  27. }
  28. catch (customException $e)
  29. {
  30. //display custom message
  31. echo $e>errorMessage();
  32. }
  33. ?>
  34. /*** 来自编程之家 jb51.cc(jb51.cc) ***/
这个新的类是旧的 exception 类的副本外加 errorMessage() 函数正因为它是旧类的副本因此它从旧类继承了属性方法我们可以使用 exception 类的方法比如 getLine() getFile() 以及 getMessage()

猜你在找的PHP相关文章