程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> php異常處理方法實例匯總

php異常處理方法實例匯總

編輯:關於PHP編程

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

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

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

    1. 上一頁:
    2. 下一頁:
    Copyright © 程式師世界 All Rights Reserved