IT貓撲網(wǎng):您身邊最放心的安全下載站! 最新更新|軟件分類|軟件專題|手機版|論壇轉貼|軟件發(fā)布

您當前所在位置:首頁網(wǎng)絡編程PHP編程 → 詳細介紹php5編程中的異常處理

詳細介紹php5編程中的異常處理

時間:2015/6/28來源:IT貓撲網(wǎng)作者:網(wǎng)管聯(lián)盟我要評論(0)

1 首先是try,catch

<?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);
}
}
?>
注意用$e->getMessage()輸出異常信息. 

2 輸出異常完整信息

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

try
{
file_open($path); //嘗試打開文件
}
catch(Exception $e)
{
echo "異常信息:".$e->getMessage()."\\n"; //返回用戶自定義的異常信息
echo "異常代碼:".$e->getcode()."\\n"; //返回用戶自定義的異常代碼
echo "文件名:".$e->getFile()."\\n"; //返回發(fā)生異常的PHP程序文件名
echo "異常代碼所在行".$e->getLine()."\\n"; //返回發(fā)生異常的代碼所在行的行號
echo "傳遞路線:";
print_r($e->getTrace()); //以數(shù)組形式返回跟蹤異常每一步傳遞的路線
echo $e->getTraceAsString(); //返回格式化成字符串的getTrace函數(shù)信息
}

function file_open($path)
{
if(!file_exists($path)) //如果文件不存在,則拋出錯誤
{
throw new Exception("文件無法找到", 1);
}

if(!fopen($path, "r"))
{
throw new Exception("文件無法打開", 2);
}
}
?> 

 

 擴展異常,即自定義異常

<?php
class FileExistsException extends Exception{} //用于處理文件不存在異常的類
class FileOpenException extends Exception{} //用于處理文件不可讀異常的類

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

try
{
file_open($path);
}
catch(FileExistsException $e) //如果產生FileExistsException異常則提示用戶確認文件位置
{
echo "程序在運行過程中發(fā)生了異常:".$e->getMessage()."\\n";
echo "請確認文件位置。";
}
catch(FileOpenException $e) //如果產生FileOpenException異常則提示用戶確認文件的可讀性
{
echo "程序在運行過程中發(fā)生了異常:".$e->getMessage()."\\n";
echo "請確認文件的可讀性。";
}
catch(Exception $e)
{
echo "[未知異常]";
echo "異常信息:".$e->getMessage()."\\n"; //返回用戶自定義的異常信息
echo "異常代碼:".$e->getCode()."\\n"; //返回用戶自定義的異常代碼
echo "文件名:".$e->getFile()."\\n"; //返回發(fā)生異常的PHP程序文件名
echo "異常代碼所在行".$e->getLine()."\\n"; //返回發(fā)生異常的代碼所在行的行號
echo "傳遞路線:";
print_r($e->getTrace()); //以數(shù)組形式返回跟蹤異常每一步傳遞的路線
echo $e->getTraceAsString(); //返回格式化成字符串的getTrace函數(shù)信息
}

function file_open($path)
{
if(!file_exists($path))
{
throw new FileExistsException("文件無法找到", 1); //拋出FileExistsException異常對象
}

if(!fopen($path, "r"))
{
throw new FileOpenException("文件無法打開", 2); //拋出FileOpenException異常對象

}
}
?> 

4 重拋異常給上層

<?php
class FileExistsException extends Exception{} //用于處理文件不存在異常的類
class FileOpenException extends Exception{} //用于處理文件不可讀異常的類

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

try
{
file_open($path);
}
catch(FileExistsException $e) //如果產生FileExistsException異常則提示用戶確認文件位置
{
echo "程序在運行過程中發(fā)生了異常:".$e->getMessage()."\\n";
echo "請確認文件位置。";
}
catch(FileOpenException $e) //如果產生FileOpenException異常則提示用戶確認文件的可讀性
{
echo "程序在運行過程中發(fā)生了異常:".$e->getMessage()."\\n";
echo "請確認文件的可讀性。";
}
catch(Exception $e)
{
echo "[未知異常]";
echo "異常信息:".$e->getMessage()."\\n"; //返回用戶自定義的異常信息
echo "異常代碼:".$e->getCode()."\\n"; //返回用戶自定義的異常代碼
echo "文件名:".$e->getFile()."\\n"; //返回發(fā)生異常的PHP程序文件名
echo "異常代碼所在行".$e->getLine()."\\n"; //返回發(fā)生異常的代碼所在行的行號
echo "傳遞路線:";
print_r($e->getTrace()); //以數(shù)組形式返回跟蹤異常每一步傳遞的路線
echo $e->getTraceAsString(); //返回格式化成字符串的getTrace函數(shù)信息
}

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函數(shù)在運行過程中出現(xiàn)異常";
throw $e; //重擲異常
}
}
?>

關鍵詞標簽:異常,處理,編程,介紹,

相關閱讀

文章評論
發(fā)表評論

熱門文章 plsql developer怎么連接數(shù)據(jù)庫-plsql deveplsql developer怎么連接數(shù)據(jù)庫-plsql deve2021年最好用的10款php開發(fā)工具推薦2021年最好用的10款php開發(fā)工具推薦在 PHP 中使用命令行工具在 PHP 中使用命令行工具php應用程序安全防范技術研究php應用程序安全防范技術研究

相關下載

人氣排行 詳解ucenter原理及第三方應用程序整合思路、方法PHP中防止SQL注入攻擊PHP會話Session的具體使用方法解析PHP運行出現(xiàn)Notice : Use of undefined constant 的解決辦法CakePHP程序員必須知道的21條技巧PHP采集圖片實例(PHP采集)PHP如何清空mySQL數(shù)據(jù)庫plsql developer怎么連接數(shù)據(jù)庫-plsql developer連接數(shù)據(jù)庫方法