国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

php異常處理方法實例匯總

2024-05-04 23:36:49
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了php異常處理方法,實例匯總了常見的php異常處理技巧,非常具有實用價值,需要的朋友可以參考下

本文實例講述了php異常處理方法。分享給大家供大家參考。具體如下:

 

 
  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.  
  12. function file_open($path)  
  13. {  
  14. if(!file_exists($path)) 
  15. //如果文件無法找到,拋出異常對象  
  16. {  
  17. throw new Exception("文件無法找到", 1);  
  18. }  
  19. if(!fopen($path"r")) 
  20. //如果文件無法打開,拋出異常對象  
  21. {  
  22. throw new Exception("文件無法打開", 2);  
  23. }  
  24. }  
  25. ?>  

 

 
  1. <?php  
  2. $path = "D://in.txt"
  3. //文件所在路徑  
  4. file_open($path); 
  5. //調用file_open函數  
  6.  
  7. function file_open($path)  
  8. {  
  9. if(!file_exists($path)) 
  10. //如果文件無法找到,拋出異常對象  
  11. {  
  12. throw new Exception("文件無法找到", 1);  
  13. }  
  14. if(!fopen($path"r")) 
  15. //如果文件無法打開,拋出異常對象  
  16. {  
  17. throw new Exception("文件無法打開", 2);  
  18. }  
  19. }  
  20. ?>  
  1. <?php  
  2. function exception_handler($e
  3. //用于處理異常的函數  
  4. {  
  5. echo "未捕獲的異常:".$e->getMessage();  
  6. }  
  7.  
  8. set_exception_handler("exception_handler"); 
  9. //設置異常處理函數  
  10. try //檢測異常  
  11. {  
  12. $path = "D://in.txt";  
  13. }  
  14. catch(Exception $e//捕獲異常  
  15. {  
  16. echo $e->getMessage();  
  17. }  
  18. file_open($path); //調用函數打開文件  
  19. function file_open($path)  
  20. {  
  21. if(!file_exists($path)) 
  22. //如果文件無法找到,拋出異常對象  
  23. {  
  24. throw new Exception("文件無法找到", 1);  
  25. }  
  26. if(!fopen($path"r")) 
  27. //如果文件無法打開,拋出異常對象  
  28. {  
  29. throw new Exception("文件無法打開", 2);  
  30. }  
  31. }  
  32. ?>  

 

  1. <?php  
  2. $path = "D://in.txt";  
  3.  
  4. try 
  5. {  
  6. file_open($path); //嘗試打開文件  
  7. }  
  8. catch(Exception $e)  
  9. {  
  10. echo "異常信息:".$e->getMessage()."/n"
  11. //返回用戶自定義的異常信息  
  12. echo "異常代碼:".$e->getCode()."/n"
  13. //返回用戶自定義的異常代碼  
  14. echo "文件名:".$e->getFile()."/n"
  15. //返回發生異常的PHP程序文件名  
  16. echo "異常代碼所在行".$e->getLine()."/n"
  17. //返回發生異常的代碼所在行的行號  
  18. echo "傳遞路線:";  
  19. print_r($e->getTrace()); 
  20. //以數組形式返回跟蹤異常每一步傳遞的路線  
  21. echo $e->getTraceAsString(); 
  22. //返回格式化成字符串的getTrace函數信息  
  23. }  
  24. function file_open($path)  
  25. {  
  26. if(!file_exists($path)) 
  27. //如果文件不存在,則拋出錯誤  
  28. {  
  29. throw new Exception("文件無法找到", 1);  
  30. }  
  31.  
  32. if(!fopen($path"r"))  
  33. {  
  34. throw new Exception("文件無法打開", 2);  
  35. }  
  36. }  
  37. ?>  

 

 
  1. <?php  
  2. class FileExistsException extends Exception{} 
  3. //用于處理文件不存在異常的類  
  4. class FileOpenException extends Exception{} 
  5. //用于處理文件不可讀異常的類  
  6. $path = "D://in.txt";  
  7. try 
  8. {  
  9. file_open($path);  
  10. }  
  11. catch(FileExistsException $e
  12. //如果產生FileExistsException異常則提示用戶確認文件位置  
  13. {  
  14. echo "程序在運行過程中發生了異常:".$e->getMessage()."/n";  
  15. echo "請確認文件位置。";  
  16. }  
  17. catch(FileOpenException $e
  18. //如果產生FileOpenException異常則提示用戶確認文件的可讀性  
  19. {  
  20. echo "程序在運行過程中發生了異常:".$e->getMessage()."/n";  
  21. echo "請確認文件的可讀性。";  
  22. }  
  23. catch(Exception $e)  
  24. {  
  25. echo "[未知異常]";  
  26. echo "異常信息:".$e->getMessage()."/n"
  27. //返回用戶自定義的異常信息  
  28. echo "異常代碼:".$e->getCode()."/n"
  29. //返回用戶自定義的異常代碼  
  30. echo "文件名:".$e->getFile()."/n"
  31. //返回發生異常的PHP程序文件名  
  32. echo "異常代碼所在行".$e->getLine()."/n"
  33. //返回發生異常的代碼所在行的行號  
  34. echo "傳遞路線:";  
  35. print_r($e->getTrace()); 
  36. //以數組形式返回跟蹤異常每一步傳遞的路線  
  37. echo $e->getTraceAsString(); 
  38. //返回格式化成字符串的getTrace函數信息  
  39. }  
  40. function file_open($path)  
  41. {  
  42. if(!file_exists($path))  
  43. {  
  44. throw new FileExistsException("文件無法找到", 1); 
  45. //拋出FileExistsException異常對象  
  46. }  
  47.  
  48. if(!fopen($path"r"))  
  49. {  
  50. throw new FileOpenException("文件無法打開", 2); 
  51. //拋出FileOpenException異常對象  
  52.  
  53. }  
  54. }  
  55. ?>  

 

 
  1. <?php  
  2. class FileExistsException extends Exception{} 
  3. //用于處理文件不存在異常的類  
  4. class FileOpenException extends Exception{} 
  5. //用于處理文件不可讀異常的類  
  6. $path = "D://in.txt";  
  7. try 
  8. {  
  9. file_open($path); //嘗試打開文件  
  10. }  
  11. catch(Exception $e)  
  12. {  
  13. echo "[未知異常]";  
  14. echo "異常信息:".$e->getMessage()."/n"
  15. //返回用戶自定義的異常信息  
  16. echo "異常代碼:".$e->getCode()."/n"
  17. //返回用戶自定義的異常代碼  
  18. echo "文件名:".$e->getFile()."/n"
  19. //返回發生異常的PHP程序文件名  
  20. echo "異常代碼所在行".$e->getLine()."/n"
  21. //返回發生異常的代碼所在行的行號  
  22. echo "傳遞路線:";  
  23. print_r($e->getTrace()); 
  24. //以數組形式返回跟蹤異常每一步傳遞的路線  
  25. echo $e->getTraceAsString(); 
  26. //返回格式化成字符串的getTrace函數信息  
  27. }  
  28. catch(FileExistsException $e
  29. //如果產生FileExistsException異常則提示用戶確認文件位置  
  30. {  
  31. echo "程序在運行過程中發生了異常:".$e->getMessage()."/n";  
  32. echo "請確認文件位置。";  
  33. }  
  34. catch(FileOpenException $e
  35. //如果產生FileOpenException異常則提示用戶確認文件的可讀性  
  36. {  
  37. echo "程序在運行過程中發生了異常:".$e->getMessage()."/n";  
  38. echo "請確認文件的可讀性。";  
  39. }  
  40. function file_open($path)  
  41. {  
  42. if(!file_exists($path)) 
  43. //如果文件不存在,則輸出錯誤  
  44. {  
  45. throw new FileExistsException("文件無法找到", 1);  
  46. }  
  47.  
  48. if(!fopen($path"r"))  
  49. {  
  50. throw new FileOpenException("文件無法打開", 2);  
  51. }  
  52. }  
  53. ?>  

 

 
  1. <?php  
  2. class FileExistsException extends Exception{} 
  3. //用于處理文件不存在異常的類  
  4. class FileOpenException extends Exception{} 
  5. //用于處理文件不可讀異常的類  
  6. $path = "D://in.txt";  
  7. try 
  8. {  
  9. file_open($path);  
  10. }  
  11. catch(FileExistsException $e
  12. //如果產生FileExistsException異常則提示用戶確認文件位置  
  13. {  
  14. echo "程序在運行過程中發生了異常:".$e->getMessage()."/n";  
  15. echo "請確認文件位置。";  
  16. }  
  17. catch(FileOpenException $e
  18. //如果產生FileOpenException異常則提示用戶確認文件的可讀性  
  19. {  
  20. echo "程序在運行過程中發生了異常:".$e->getMessage()."/n";  
  21. echo "請確認文件的可讀性。";  
  22. }  
  23. catch(Exception $e)  
  24. {  
  25. echo "[未知異常]";  
  26. echo "異常信息:".$e->getMessage()."/n"
  27. //返回用戶自定義的異常信息  
  28. echo "異常代碼:".$e->getCode()."/n"
  29. //返回用戶自定義的異常代碼  
  30. echo "文件名:".$e->getFile()."/n"
  31. //返回發生異常的PHP程序文件名  
  32. echo "異常代碼所在行".$e->getLine()."/n"
  33. //返回發生異常的代碼所在行的行號  
  34. echo "傳遞路線:";  
  35. print_r($e->getTrace()); 
  36. //以數組形式返回跟蹤異常每一步傳遞的路線  
  37. echo $e->getTraceAsString(); 
  38. //返回格式化成字符串的getTrace函數信息  
  39. }  
  40. function file_open($path)  
  41. {  
  42. try 
  43. {  
  44. if(!file_exists($path))  
  45. {  
  46. throw new FileExistsException("文件無法找到", 1);  
  47. }  
  48.  
  49. if(!fopen($path"r"))  
  50. {  
  51. throw new FileOpenException("文件無法打開", 2);  
  52. }  
  53. }  
  54. catch(Exception $e//捕獲異常  
  55. {  
  56. echo "file_open函數在運行過程中出現異常";  
  57. throw $e//重擲異常  
  58. }  
  59. }  
  60. ?> 

希望本文所述對大家的php程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 乌兰浩特市| 铁力市| 精河县| 嵩明县| 上蔡县| 万荣县| 饶河县| 郴州市| 齐河县| 正阳县| 芒康县| 金寨县| 当雄县| 宁远县| 中超| 衡南县| 九龙县| 金昌市| 马公市| 耿马| 乌拉特中旗| 县级市| 银川市| 阆中市| 桃江县| 徐汇区| 韩城市| 绍兴市| 中卫市| 金平| 包头市| 自治县| 汤原县| 宣恩县| 苏尼特右旗| 孟津县| 监利县| 西丰县| 华宁县| 威宁| 双鸭山市|