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

首頁 > 開發 > PHP > 正文

php5異常處理詳解

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

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()."http://n"; //返回用戶自定義的異常信息
echo "異常代碼:".$e->getcode()."http://n"; //返回用戶自定義的異常代碼
echo "文件名:".$e->getfile()."http://n"; //返回發生異常的php程序文件名
echo "異常代碼所在行".$e->getline()."http://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);
}
}
?> 


 擴展異常,即自定義異常

<?php
class fileexistsexception extends exception{} //用于處理文件不存在異常的類
class fileopenexception extends exception{} //用于處理文件不可讀異常的類

$path = "d:////in.txt";

try
{
file_open($path);
}
catch(fileexistsexception $e) //如果產生fileexistsexception異常則提示用戶確認文件位置
{
echo "程序在運行過程中發生了異常:".$e->getmessage()."http://n";
echo "請確認文件位置。";
}
catch(fileopenexception $e) //如果產生fileopenexception異常則提示用戶確認文件的可讀性
{
echo "程序在運行過程中發生了異常:".$e->getmessage()."http://n";
echo "請確認文件的可讀性。";
}
catch(exception $e)
{
echo "[未知異常]";
echo "異常信息:".$e->getmessage()."http://n"; //返回用戶自定義的異常信息
echo "異常代碼:".$e->getcode()."http://n"; //返回用戶自定義的異常代碼
echo "文件名:".$e->getfile()."http://n"; //返回發生異常的php程序文件名
echo "異常代碼所在行".$e->getline()."http://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異常對象

}
}
?> 

4 重拋異常給上層

<?php
class fileexistsexception extends exception{} //用于處理文件不存在異常的類
class fileopenexception extends exception{} //用于處理文件不可讀異常的類

$path = "d:////in.txt";

try
{
file_open($path);
}
catch(fileexistsexception $e) //如果產生fileexistsexception異常則提示用戶確認文件位置
{
echo "程序在運行過程中發生了異常:".$e->getmessage()."http://n";
echo "請確認文件位置。";
}
catch(fileopenexception $e) //如果產生fileopenexception異常則提示用戶確認文件的可讀性
{
echo "程序在運行過程中發生了異常:".$e->getmessage()."http://n";
echo "請確認文件的可讀性。";
}
catch(exception $e)
{
echo "[未知異常]";
echo "異常信息:".$e->getmessage()."http://n"; //返回用戶自定義的異常信息
echo "異常代碼:".$e->getcode()."http://n"; //返回用戶自定義的異常代碼
echo "文件名:".$e->getfile()."http://n"; //返回發生異常的php程序文件名
echo "異常代碼所在行".$e->getline()."http://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; //重擲異常
}
}
?>

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 麟游县| 浦北县| 肇源县| 堆龙德庆县| 兰考县| 扬州市| 辉南县| 六盘水市| 科尔| 神农架林区| 天全县| 保亭| 饶阳县| 乌鲁木齐市| 花垣县| 东阿县| 德惠市| 赤城县| 津市市| 湖口县| 金沙县| 沙河市| 岳阳市| 广饶县| 江达县| 达日县| 丹寨县| 英山县| 蓝山县| 吉木乃县| 长沙县| 仪陇县| 巴南区| 武威市| 丹寨县| 山东省| 汝阳县| 林口县| 绍兴县| 留坝县| 平安县|