Discuz教程网

PHP 异常处理实现代码

[复制链接]
authicon dly 发表于 2011-9-10 20:23:47 | 显示全部楼层 |阅读模式
代码如下:

  1. <?php
  2. $path = "D:\\in.txt";
  3. try //检测异常
  4. {
  5. file_open($path);
  6. }
  7. catch(Exception $e) //捕获异常
  8. {
  9. echo $e->getMessage();
  10. }

  11. function file_open($path)
  12. {
  13. if(!file_exists($path)) //如果文件无法找到,抛出异常对象
  14. {
  15. throw new Exception("文件无法找到", 1);
  16. }

  17. if(!fopen($path, "r")) //如果文件无法打开,抛出异常对象
  18. {
  19. throw new Exception("文件无法打开", 2);
  20. }
  21. }
  22. ?>
复制代码




代码如下:


  1. <?php
  2. $path = "D:\\in.txt"; //文件所在路径
  3. file_open($path); //调用file_open函数

  4. function file_open($path)
  5. {
  6. if(!file_exists($path)) //如果文件无法找到,抛出异常对象
  7. {
  8. throw new Exception("文件无法找到", 1);
  9. }

  10. if(!fopen($path, "r")) //如果文件无法打开,抛出异常对象
  11. {
  12. throw new Exception("文件无法打开", 2);
  13. }
  14. }
  15. ?>



  16. 代码如下:

  17. <?php
  18. function exception_handler($e) //用于处理异常的函数
  19. {
  20. echo "未捕获的异常:".$e->getMessage();
  21. }

  22. set_exception_handler("exception_handler"); //设置异常处理函数

  23. try //检测异常
  24. {
  25. $path = "D:\\in.txt";
  26. }
  27. catch(Exception $e) //捕获异常
  28. {
  29. echo $e->getMessage();
  30. }

  31. file_open($path); //调用函数打开文件

  32. function file_open($path)
  33. {
  34. if(!file_exists($path)) //如果文件无法找到,抛出异常对象
  35. {
  36. throw new Exception("文件无法找到", 1);
  37. }

  38. if(!fopen($path, "r")) //如果文件无法打开,抛出异常对象
  39. {
  40. throw new Exception("文件无法打开", 2);
  41. }
  42. }
  43. ?>
复制代码




代码如下:

  1. <?php
  2. $path = "D:\\in.txt";

  3. try
  4. {
  5. file_open($path); //尝试打开文件
  6. }
  7. catch(Exception $e)
  8. {
  9. echo "异常信息:".$e->getMessage()."\n"; //返回用户自定义的异常信息
  10. echo "异常代码:".$e->getCode()."\n"; //返回用户自定义的异常代码
  11. echo "文件名:".$e->getFile()."\n"; //返回发生异常的PHP程序文件名
  12. echo "异常代码所在行".$e->getLine()."\n"; //返回发生异常的代码所在行的行号
  13. echo "传递路线:";
  14. print_r($e->getTrace()); //以数组形式返回跟踪异常每一步传递的路线
  15. echo $e->getTraceAsString(); //返回格式化成字符串的getTrace函数信息
  16. }

  17. function file_open($path)
  18. {
  19. if(!file_exists($path)) //如果文件不存在,则抛出错误
  20. {
  21. throw new Exception("文件无法找到", 1);
  22. }

  23. if(!fopen($path, "r"))
  24. {
  25. throw new Exception("文件无法打开", 2);
  26. }
  27. }
  28. ?>
复制代码





代码如下:

  1. <?php
  2. class FileExistsException extends Exception{} //用于处理文件不存在异常的类
  3. class FileOpenException extends Exception{} //用于处理文件不可读异常的类

  4. $path = "D:\\in.txt";

  5. try
  6. {
  7. file_open($path);
  8. }
  9. catch(FileExistsException $e) //如果产生FileExistsException异常则提示用户确认文件位置
  10. {
  11. echo "程序在运行过程中发生了异常:".$e->getMessage()."\n";
  12. echo "请确认文件位置。";
  13. }
  14. catch(FileOpenException $e) //如果产生FileOpenException异常则提示用户确认文件的可读性
  15. {
  16. echo "程序在运行过程中发生了异常:".$e->getMessage()."\n";
  17. echo "请确认文件的可读性。";
  18. }
  19. catch(Exception $e)
  20. {
  21. echo "[未知异常]";
  22. echo "异常信息:".$e->getMessage()."\n"; //返回用户自定义的异常信息
  23. echo "异常代码:".$e->getCode()."\n"; //返回用户自定义的异常代码
  24. echo "文件名:".$e->getFile()."\n"; //返回发生异常的PHP程序文件名
  25. echo "异常代码所在行".$e->getLine()."\n"; //返回发生异常的代码所在行的行号
  26. echo "传递路线:";
  27. print_r($e->getTrace()); //以数组形式返回跟踪异常每一步传递的路线
  28. echo $e->getTraceAsString(); //返回格式化成字符串的getTrace函数信息
  29. }

  30. function file_open($path)
  31. {
  32. if(!file_exists($path))
  33. {
  34. throw new FileExistsException("文件无法找到", 1); //抛出FileExistsException异常对象
  35. }

  36. if(!fopen($path, "r"))
  37. {
  38. throw new FileOpenException("文件无法打开", 2); //抛出FileOpenException异常对象

  39. }
  40. }
  41. ?>
复制代码




代码如下:

  1. <?php
  2. class FileExistsException extends Exception{} //用于处理文件不存在异常的类
  3. class FileOpenException extends Exception{} //用于处理文件不可读异常的类

  4. $path = "D:\\in.txt";

  5. try
  6. {
  7. file_open($path); //尝试打开文件
  8. }
  9. catch(Exception $e)
  10. {
  11. echo "[未知异常]";
  12. echo "异常信息:".$e->getMessage()."\n"; //返回用户自定义的异常信息
  13. echo "异常代码:".$e->getCode()."\n"; //返回用户自定义的异常代码
  14. echo "文件名:".$e->getFile()."\n"; //返回发生异常的PHP程序文件名
  15. echo "异常代码所在行".$e->getLine()."\n"; //返回发生异常的代码所在行的行号
  16. echo "传递路线:";
  17. print_r($e->getTrace()); //以数组形式返回跟踪异常每一步传递的路线
  18. echo $e->getTraceAsString(); //返回格式化成字符串的getTrace函数信息
  19. }
  20. catch(FileExistsException $e) //如果产生FileExistsException异常则提示用户确认文件位置
  21. {
  22. echo "程序在运行过程中发生了异常:".$e->getMessage()."\n";
  23. echo "请确认文件位置。";
  24. }
  25. catch(FileOpenException $e) //如果产生FileOpenException异常则提示用户确认文件的可读性
  26. {
  27. echo "程序在运行过程中发生了异常:".$e->getMessage()."\n";
  28. echo "请确认文件的可读性。";
  29. }

  30. function file_open($path)
  31. {
  32. if(!file_exists($path)) //如果文件不存在,则输出错误
  33. {
  34. throw new FileExistsException("文件无法找到", 1);
  35. }

  36. if(!fopen($path, "r"))
  37. {
  38. throw new FileOpenException("文件无法打开", 2);
  39. }
  40. }
  41. ?>
复制代码




代码如下:

  1. <?php
  2. class FileExistsException extends Exception{} //用于处理文件不存在异常的类
  3. class FileOpenException extends Exception{} //用于处理文件不可读异常的类

  4. $path = "D:\\in.txt";

  5. try
  6. {
  7. file_open($path);
  8. }
  9. catch(FileExistsException $e) //如果产生FileExistsException异常则提示用户确认文件位置
  10. {
  11. echo "程序在运行过程中发生了异常:".$e->getMessage()."\n";
  12. echo "请确认文件位置。";
  13. }
  14. catch(FileOpenException $e) //如果产生FileOpenException异常则提示用户确认文件的可读性
  15. {
  16. echo "程序在运行过程中发生了异常:".$e->getMessage()."\n";
  17. echo "请确认文件的可读性。";
  18. }
  19. catch(Exception $e)
  20. {
  21. echo "[未知异常]";
  22. echo "异常信息:".$e->getMessage()."\n"; //返回用户自定义的异常信息
  23. echo "异常代码:".$e->getCode()."\n"; //返回用户自定义的异常代码
  24. echo "文件名:".$e->getFile()."\n"; //返回发生异常的PHP程序文件名
  25. echo "异常代码所在行".$e->getLine()."\n"; //返回发生异常的代码所在行的行号
  26. echo "传递路线:";
  27. print_r($e->getTrace()); //以数组形式返回跟踪异常每一步传递的路线
  28. echo $e->getTraceAsString(); //返回格式化成字符串的getTrace函数信息
  29. }

  30. function file_open($path)
  31. {
  32. try
  33. {
  34. if(!file_exists($path))
  35. {
  36. throw new FileExistsException("文件无法找到", 1);
  37. }

  38. if(!fopen($path, "r"))
  39. {
  40. throw new FileOpenException("文件无法打开", 2);
  41. }
  42. }
  43. catch(Exception $e) //捕获异常
  44. {
  45. echo "file_open函数在运行过程中出现异常";
  46. throw $e; //重掷异常
  47. }
  48. }
  49. ?>
复制代码





上一篇:PHP 表单数据的获取代码
下一篇:PHP session 预定义数组
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-6-19 06:13

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表