getMessage(); } function file_open($path) { if(!file_exists($path"/> getMessage(); } function file_open($path) { if(!file_exists($path">

首頁  >  文章  >  後端開發  >  php5 php5程式設計中的異常處理詳細方法介紹

php5 php5程式設計中的異常處理詳細方法介紹

WBOY
WBOY原創
2016-07-29 08:38:30778瀏覽

1 首先是try,catch 
$path = "D:\\in.txt"; 
try //偵測異常 

file_open($path); 🎜>} 
catch(Exception $e) //捕獲異常 

echo $e->getMessage(); 

function 🎜>if(!file_exists($path)) //如果檔案找不到,拋出例外物件 

throw new Exception("檔案無法找到", 1); 

if (!fopen($path, "r")) //如果檔案無法打開,則拋出例外物件 

throw new Exception("檔案無法開啟", 2); 


?> 
注意用$e->getMessage()輸出異常訊息. 
2 輸出異常完整資訊 
$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($) 
 

if(!file_exists($path)) //如果檔案不存在,則拋出錯誤 

throw new Exception("檔案無法找到", 1); 
new Exception("檔案無法找到", 1); 
} >if(!fopen($path, "r")) 

throw new Exception("檔案無法開啟", 2); 


?擴充異常,即自訂異常 
class FileExistsException extends Exception{} //用於處理檔案不存在異常的類別 
class FileOpenException異常的類別 
$path = "D:\\in.txt"; 
try 

file_open($path); 

catch(File /如果產生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(Fpathd )) 

throw new FileExistsException("檔案無法找到", 1); //拋出FileExistsException異常物件 

if(!openfopen($ 🎜>{ 
throw new FileOpenException("檔案無法開啟", 2); //拋出FileOpenException異常物件 


?> >🎜>} 

?> > >class FileExistsException extends Exception{} //用於處理檔案不存在異常的類別 
class FileOpenException extends Exception{ //用於處理檔案中不可讀異常的類別不易讀"D:\\in.txt"; 
try 

file_open($path); 

catch(FileExistsException $e)Exp///如果產生File檔案位置 

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) 

 file_open($path) 

 > >if(!file_exists($path)) 

throw new FileExistsException("檔案無法找到", 1); 

if(!fopen($path, 

if(!fopen($path, "r"))

throw new FileOpenException("檔案無法開啟", 2); 


catch(Exception $cho> file_open函數在運作過程中出現異常"; 
throw $e; //重擲異常 


?>

以上就介紹了php5 php5程式設計的異常處理詳細方法介紹,包含了php5方面的內容,希望對PHP教學有興趣的朋友有幫助。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn