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

首頁 > 網(wǎng)站 > 建站經(jīng)驗 > 正文

php異常處理方法實例匯總

2024-04-25 20:37:42
字體:
供稿:網(wǎng)友

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

<?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);

}

}

?>

<?php

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

//文件所在路徑

file_open($path);

//調(diào)用file_open函數(shù)

function file_open($path)

{

if(!file_exists($path))

//如果文件無法找到,拋出異常對象

{

throw new Exception("文件無法找到", 1);

}

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

//如果文件無法打開,拋出異常對象

{

throw new Exception("文件無法打開", 2);

}

}

?>

<?php

function exception_handler($e)

//用于處理異常的函數(shù)

{

echo "未捕獲的異常:".$e->getMessage();

}

set_exception_handler("exception_handler");

//設(shè)置異常處理函數(shù)

try //檢測異常

{

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

}

catch(Exception $e) //捕獲異常

{

echo $e->getMessage();

}

file_open($path); //調(diào)用函數(shù)打開文件

function file_open($path)

{

if(!file_exists($path))

//如果文件無法找到,拋出異常對象

{

throw new Exception("文件無法找到", 1);

}

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

//如果文件無法打開,拋出異常對象

{

throw new Exception("文件無法打開", 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)

//如果產(chǎn)生FileExistsException異常則提示用戶確認文件位置

{

echo "程序在運行過程中發(fā)生了異常:".$e->getMessage()."/n";

echo "請確認文件位置。";

}

catch(FileOpenException $e)

//如果產(chǎn)生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異常對象

}

}

?>

<?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";

//返回發(fā)生異常的PHP程序文件名

echo "異常代碼所在行".$e->getLine()."/n";

//返回發(fā)生異常的代碼所在行的行號

echo "傳遞路線:";

print_r($e->getTrace());

//以數(shù)組形式返回跟蹤異常每一步傳遞的路線

echo $e->getTraceAsString();

//返回格式化成字符串的getTrace函數(shù)信息

}

catch(FileExistsException $e)

//如果產(chǎn)生FileExistsException異常則提示用戶確認文件位置

{

echo "程序在運行過程中發(fā)生了異常:".$e->getMessage()."/n";

echo "請確認文件位置。";

}

catch(FileOpenException $e)

//如果產(chǎn)生FileOpenException異常則提示用戶確認文件的可讀性

{

echo "程序在運行過程中發(fā)生了異常:".$e->getMessage()."/n";

echo "請確認文件的可讀性。";

}

function file_open($path)

{

if(!file_exists($path))

//如果文件不存在,則輸出錯誤

{

throw new FileExistsException("文件無法找到", 1);

}

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

{

throw new FileOpenException("文件無法打開", 2);

}

}

?>

<?php

class FileExistsException extends Exception{}

//用于處理文件不存在異常的類

class FileOpenException extends Exception{}

//用于處理文件不可讀異常的類

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

try

{

file_open($path);

}

catch(FileExistsException $e)

//如果產(chǎn)生FileExistsException異常則提示用戶確認文件位置

{

echo "程序在運行過程中發(fā)生了異常:".$e->getMessage()."/n";

echo "請確認文件位置。";

}

catch(FileOpenException $e)

//如果產(chǎn)生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; //重擲異常

}

}

?>

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

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 汶上县| 桓仁| 东乡族自治县| 施甸县| 永春县| 桐梓县| 阜阳市| 福贡县| 柘荣县| 莫力| 信宜市| 菏泽市| 阳春市| 黔江区| 咸宁市| 安阳县| 虎林市| 天气| 太仓市| 南华县| 浦城县| 建水县| 泰顺县| 新龙县| 屏边| 七台河市| 剑阁县| 丽水市| 龙井市| 彭阳县| 濉溪县| 沽源县| 静乐县| 北票市| 阿拉善盟| 托克逊县| 巴青县| 岑溪市| 噶尔县| 和林格尔县| 教育|