php5中的異常小結

2021-08-29 09:26:45 字數 1423 閱讀 9532

php5中有了異常了,比以前有大的改進,下面筆記之.

1 首先是try,catch

<?php

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

try           //檢測異常

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

function file_open($path)

if(!fopen($path, "r"))       //如果檔案無法開啟,丟擲異常物件

}?>

注意用$e->getmessage()輸出異常資訊.

2 輸出異常完整資訊

<?php

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

trycatch(exception $e)

function file_open($path)

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

}?>

3  擴充套件異常,即自定義異常

<?php

class fileexist***ception extends exception{}  //用於處理檔案不存在異常的類

class fileopenexception extends exception{}  //用於處理檔案不可讀異常的類

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

trycatch(fileexist***ception $e)    //如果產生fileexist***ception異常則提示使用者確認檔案位置

catch(fileopenexception $e)    //如果產生fileopenexception異常則提示使用者確認檔案的可讀性

catch(exception $e)

function file_open($path)

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

}?>

4 重拋異常給上層

<?php

class fileexist***ception extends exception{}  //用於處理檔案不存在異常的類

class fileopenexception extends exception{}  //用於處理檔案不可讀異常的類

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

trycatch(fileexist***ception $e)    //如果產生fileexist***ception異常則提示使用者確認檔案位置

catch(fileopenexception $e)    //如果產生fileopenexception異常則提示使用者確認檔案的可讀性

catch(exception $e)

function file_open($path)

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

}catch(exception $e)        //捕獲異常

}?>

php5中的異常小結

php5中有了異常了,比以前有大的改進,下面筆記之.1 首先是try,catch path d in.txt try 檢測異常 catch exception e 捕獲異常 function file open path if fopen path,r 如果檔案無法開啟,丟擲異常物件 注意用 e g...

php5中的異常小結

php5中有了異常了,比以前有大的改進,下面筆記之.1 首先是try,catch path d in.txt try 檢測異常 catch exception e 捕獲異常 function file open path if fopen path,r 如果檔案無法開啟,丟擲異常物件 注意用 e g...

PHP5中的異常處理詳解

首先是try,catch php path d in.txt try 檢測異常 catch exception e 捕獲異常 function file open path if fopen path,r 如果檔案無法開啟,丟擲異常物件 注意用 e getmessage 輸出異常資訊.輸出異常完整資...