


php錯誤處理和異常處理機制
添加時間:2014-7-4 3:17:21
添加:
思海網絡
當我們開發(fā)程序時,有時候程序出現了問題,我們就可以用以下幾種辦法找出錯誤。
開發(fā)階段:開發(fā)時輸出所有的錯誤報告,有利于我們進行程序調試
運行階段:我們不要讓程序輸出任何一種錯誤報告(不能讓用戶看到(包括懂技術, 不懂技術的人))
將錯誤報告寫入日志中
一、指定錯誤報告 error_reporting = E_LL
二、關閉錯誤輸出 display_errors = Off
三、開啟錯誤日志功能 log_errors = On
1. 默認如果不指定錯誤日志位置,則默認寫WEB服務器的日志中
2. 為error_log選項指定 一個文件名(可寫)
3. 寫入到操作系統(tǒng)日志中error_log=syslog
以下代碼示例
";
?>
當然php還提供了函數error_get_last()來獲得錯誤信息
函數定義和用法
error_get_last()函數獲取最后發(fā)生的錯誤。 該函數以數組的形式返回最后發(fā)生的錯誤。 返回的數組包含 4 個鍵和值: [type] - 錯誤類型 [message] - 錯誤消息 [file] - 發(fā)生錯誤所在的文件 [line] - 發(fā)生錯誤所在的
小例子:
輸出:
Array ( [type] => 8 [message] => Undefined variable: test [file] => D:\www\test.php [line] => 2 )
所以這樣我們也很方便了。。。這樣是不是對調試程序和排查錯誤的時候很有幫助呢?
這些錯誤報告級別是錯誤處理程序旨在處理的錯誤的不同的類型:
值 常量 描述
2 E_WARNING 非致命的 run-time 錯誤。不暫停腳本執(zhí)行。
8 E_NOTICE
Run-time 通知。
腳本發(fā)現可能有錯誤發(fā)生,但也可能在腳本正常運行時發(fā)生。
256 E_USER_ERROR 致命的用戶生成的錯誤。這類似于程序員使用 PHP 函數 trigger_error() 設置的 E_ERROR。
512 E_USER_WARNING 非致命的用戶生成的警告。這類似于程序員使用 PHP 函數 trigger_error() 設置的 E_WARNING。
1024 E_USER_NOTICE 用戶生成的通知。這類似于程序員使用 PHP 函數 trigger_error() 設置的 E_NOTICE。
4096 E_RECOVERABLE_ERROR 可捕獲的致命錯誤。類似 E_ERROR,但可被用戶定義的處理程序捕獲。(參見 set_error_handler())
8191 E_ALL
所有錯誤和警告,除級別 E_STRICT 以外。
(在 PHP 6.0,E_STRICT 是 E_ALL 的一部分)
php異常處理機制
定義:
異常處理: 意外,是在程序運行過程中發(fā)生的意料這外的事,使用異常改變腳本正常流程
語法格式:
try
{ //...}
catch(Exception $e)
{ //...}
PHP中try{}catch{}是異常處理.
將要執(zhí)行的代碼放入TRY塊中,如果這些代碼執(zhí)行過程中某一條語句發(fā)生異常,則程序直接跳轉到CATCH塊中,由$e收集錯誤信息和顯示.
PHP中try{}catch{}語句
為了進一步處理異常,我們需要使用PHP中try{}catch{}----包括Try語句和至少一個的catch語句。任何調用 可能拋出異常的方法的代碼都應該使用try語句。Catch語句用來處理可能拋出的異常。
例子:
我寫一段代碼:
自己定義一個異常類
作用:就是寫一個或多個方法解決當發(fā)生這個異常時的處理方式
1. 自己定義異常類,必須是Exception(內置類)的子類, 可以查看PHP手冊里面Exception(內置類)的使用方法
2. Exception類中的只有構造方法和toString()可以重寫, 其它都final
";
}
function open(){
touch("tmp.txt");
$file=fopen("tmp.txt", "r");
return $file;
}
}
?>
1. 如果try中代碼沒有問題,則將try中代碼執(zhí)行完后就到catch后執(zhí)行
2. 如果try中代碼有異常發(fā)生,則拋出一個異常對象(使用throw),拋出給了catch中的參數, 則在try中代碼就不會再繼續(xù)執(zhí)行下去 直接跳轉到catch中去執(zhí)行, catch中執(zhí)行完成, 再繼續(xù)向下執(zhí)行
注意: 提示發(fā)生了什么異常,這不是主要我們要做事,需要在catch中解決這個異常, 如果解決不了,則出去給用戶在下面代碼中,如果我沒有這個TMP.TXT文件的話,就會拋出異常了。
如果有異常,我們調用OPEN方法就可以解決這個異常了。
getMessage()."
"; //getMessage() 是PHP里面內置的方法,可以直接調用
$file=$e->open();
}
下面將代碼進行整理以及多個異常處理方法:
";
}
function open(){
touch("tmp.txt");
$file=fopen("tmp.txt", "r");
return $file;
}
}
class DemoException extends Exception {
function pro(){
echo "處理demo發(fā)生的異常
";
}
}
class TestException extends Exception {
function pro(){
echo "這里處理test發(fā)生的異常
";
}
}
class HelloException extends Exception {
}
class MyClass {
function openfile(){
$file=@fopen("tmp.txt", "r");
if(!$file)
throw new OpenFileException("文件打開失敗");
}
function demo($num=0){
if($num==1)
throw new DemoException("演示出異常");
}
function test($num=0){
if($num==1)
throw new TestException("測試出錯");
}
function fun($num=0){
if($num==1)
throw new HelloException("###########");
}
}
try{
echo "11111111111111
";
$my=new MyClass();
$my->openfile();
$my->demo(0);
$my->test(0);
$my->fun(1);
echo "22222222222222222
";
}catch(OpenFileException $e){ //$e =new Exception();
echo $e->getMessage()."
";
$file=$e->open();
}catch(DemoException $e){
echo $e->getMessage()."
";
$e->pro();
}catch(TestException $e){
echo $e->getMessage()."
";
$e->pro();
}catch(Exception $e){
echo $e->getMessage()."
";
}
var_dump($file);
echo "444444444444444444444
";
User Access Verification
Password:
Router>enable
Password:
Router#show ip route
Codes: C - connected, S - static, I - IGRP, R - RIP, M - mobile, B - BGP
D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area
N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
E1 - OSPF external type 1, E2 - OSPF external type 2, E - EGP
i - IS-IS, L1 - IS-IS level-1, L2 - IS-IS level-2, ia - IS-IS inter area
* - candidate default, U - per-user static route, o - ODR
P - periodic downloaded static route
Gateway of last resort is 221.4.162.254 to network 0.0.0.0
119.0.0.0/32 is subnetted, 160 subnets
S 119.63.36.108 is directly connected, Null0
S 119.63.39.111 is directly connected, Null0
S 119.63.38.107 is directly connected, Null0
S 119.63.34.111 is directly connected, Null0
S 119.63.36.106 is directly connected, Null0
S 119.63.36.107 is directly connected, Null0
S 119.63.37.100 is directly connected, Null0
S 119.63.36.101 is directly connected, Null0
S 119.63.39.98 is directly connected, Null0
S 119.63.36.98 is directly connected, Null0
S 119.63.38.127 is directly connected, Null0
S 119.63.39.123 is directly connected, Null0
S 119.63.39.122 is directly connected, Null0
S 119.63.36.117 is directly connected, Null0
S 119.63.34.112 is directly connected, Null0
S 119.63.39.117 is directly connected, Null0
S 119.63.37.119 is directly connected, Null0
S 119.63.37.118 is directly connected, Null0
S 119.63.34.118 is directly connected, Null0
S 119.63.39.115 is directly connected, Null0
S 119.252.246.98 is directly connected, Null0
S 119.63.36.77 is directly connected, Null0
S 119.63.39.78 is directly connected, Null0
S 119.63.36.79 is directly connected, Null0
S 119.252.245.91 is directly connected, Null0
S 119.63.36.75 is directly connected, Null0
S 119.63.34.66 is directly connected, Null0
S 119.63.38.71 is directly connected, Null0
S 119.63.37.65 is directly connected, Null0
S 119.63.34.69 is directly connected, Null0
S 119.63.36.93 is directly connected, Null0
S 119.63.34.88 is directly connected, Null0
S 119.63.38.93 is directly connected, Null0
S 119.63.36.89 is directly connected, Null0
S 119.63.36.86 is directly connected, Null0
S 119.63.39.82 is directly connected, Null0
S 119.63.39.80 is directly connected, Null0
S 119.63.38.47 is directly connected, Null0
S 119.63.38.42 is directly connected, Null0
S 119.63.36.42 is directly connected, Null0
S 119.63.38.41 is directly connected, Null0
S 119.63.37.37 is directly connected, Null0
S 119.63.38.34 is directly connected, Null0
S 119.63.34.39 is directly connected, Null0
S 119.63.36.35 is directly connected, Null0
S 119.63.39.32 is directly connected, Null0
S 119.63.38.62 is directly connected, Null0
S 119.63.38.63 is directly connected, Null0
S 119.252.245.45 is directly connected, Null0
S 119.63.39.60 is directly connected, Null0
S 119.63.39.58 is directly connected, Null0
S 119.63.36.58 is directly connected, Null0
S 119.63.39.55 is directly connected, Null0
S 119.63.39.52 is directly connected, Null0
S 119.63.39.51 is directly connected, Null0
S 119.63.37.51 is directly connected, Null0
S 119.63.38.14 is directly connected, Null0
S 119.63.34.9 is directly connected, Null0
S 119.63.36.8 is directly connected, Null0
S 119.63.37.9 is directly connected, Null0
S 119.63.40.6 is directly connected, Null0
S 119.63.40.8 is directly connected, Null0
S 119.63.37.4 is directly connected, Null0
S 119.63.38.3 is directly connected, Null0
S 119.63.36.2 is directly connected, Null0
S 119.63.36.29 is directly connected, Null0
S 119.63.38.31 is directly connected, Null0
S 119.63.36.30 is directly connected, Null0
S 119.63.34.30 is directly connected, Null0
S 119.63.39.27 is directly connected, Null0
S 119.63.37.25 is directly connected, Null0
S 119.63.36.27 is directly connected, Null0
S 119.63.36.20 is directly connected, Null0
S 119.63.35.18 is directly connected, Null0
S 119.63.38.20 is directly connected, Null0
S 119.63.36.22 is directly connected, Null0
S 119.63.37.22 is directly connected, Null0
S 119.63.39.20 is directly connected, Null0
S 119.63.35.16 is directly connected, Null0
S 119.63.37.17 is directly connected, Null0
S 119.63.39.19 is directly connected, Null0
S 119.63.38.16 is directly connected, Null0
S 119.63.34.20 is directly connected, Null0
S 119.63.33.23 is directly connected, Null0
S 119.63.37.239 is directly connected, Null0
S 119.63.37.238 is directly connected, Null0
S 119.63.38.234 is directly connected, Null0
S 119.63.34.237 is directly connected, Null0
S 119.63.34.225 is directly connected, Null0
S 119.63.38.226 is directly connected, Null0
S 119.252.246.239 is directly connected, Null0
S 119.63.35.254 is directly connected, Null0
S 119.252.245.232 is directly connected, Null0
S 119.63.34.243 is directly connected, Null0
S 119.63.36.246 is directly connected, Null0
S 119.63.39.244 is directly connected, Null0
S 119.63.39.243 is directly connected, Null0
S 119.63.36.205 is directly connected, Null0
S 119.63.36.206 is directly connected, Null0
S 119.63.34.204 is directly connected, Null0
S 119.63.36.203 is directly connected, Null0
S 119.63.36.197 is directly connected, Null0
S 119.63.38.199 is directly connected, Null0
S 119.63.34.198 is directly connected, Null0
S 119.63.38.194 is directly connected, Null0
S 119.63.36.220 is directly connected, Null0
S 119.63.39.221 is directly connected, Null0
S 119.63.34.217 is directly connected, Null0
S 119.63.36.217 is directly connected, Null0
S 119.63.39.217 is directly connected, Null0
S 119.63.36.218 is directly connected, Null0
S 119.63.39.215 is directly connected, Null0
S 119.63.36.213 is directly connected, Null0
S 119.63.39.212 is directly connected, Null0
S 119.63.38.213 is directly connected, Null0
S 119.63.38.209 is directly connected, Null0
S 119.63.39.175 is directly connected, Null0
S 119.63.36.174 is directly connected, Null0
S 119.63.39.172 is directly connected, Null0
S 119.63.38.173 is directly connected, Null0
S 119.63.36.168 is directly connected, Null0
S 119.63.39.170 is directly connected, Null0
S 119.63.38.171 is directly connected, Null0
S 119.63.38.168 is directly connected, Null0
S 119.63.39.168 is directly connected, Null0
S 119.63.39.167 is directly connected, Null0
S 119.63.34.163 is directly connected, Null0
S 119.63.34.160 is directly connected, Null0
S 119.63.38.165 is directly connected, Null0
S 119.63.38.190 is directly connected, Null0
S 119.63.36.184 is directly connected, Null0
S 119.63.38.187 is directly connected, Null0
S 119.63.39.186 is directly connected, Null0
S 119.252.245.167 is directly connected, Null0
S 119.63.39.182 is directly connected, Null0
S 119.63.37.177 is directly connected, Null0
S 119.63.38.179 is directly connected, Null0
S 119.252.245.160 is directly connected, Null0
S 119.63.37.143 is directly connected, Null0
S 119.63.39.138 is directly connected, Null0
S 119.63.39.137 is directly connected, Null0
S 119.63.39.136 is directly connected, Null0
S 119.63.36.139 is directly connected, Null0
S 119.63.38.132 is directly connected, Null0
S 119.63.39.132 is directly connected, Null0
S 119.63.38.133 is directly connected, Null0
S 119.63.37.129 is directly connected, Null0
S 119.63.39.130 is directly connected, Null0
S 119.63.37.131 is directly connected, Null0
S 119.63.36.131 is directly connected, Null0
S 119.63.38.158 is directly connected, Null0
S 119.63.34.155 is directly connected, Null0
S 119.63.34.152 is directly connected, Null0
S 119.63.36.158 is directly connected, Null0
S 119.63.38.157 is directly connected, Null0
S 119.63.34.159 is directly connected, Null0
S 119.63.34.157 is directly connected, Null0
S 119.63.36.149 is directly connected, Null0
S 119.63.36.150 is directly connected, Null0
S 119.63.36.151 is directly connected, Null0
221.4.1.0/32 is subnetted, 1 subnets
S 221.4.1.201 [1/0] via 221.4.162.254
112.0.0.0/32 is subnetted, 1 subnets
S 112.91.22.171 is directly connected, Null0
221.4.162.0/25 is subnetted, 1 subnets
C 221.4.162.128 is directly connected, FastEthernet0/0
183.60.0.0/32 is subnetted, 6 subnets
S 183.60.233.120 is directly connected, Null0
S 183.60.201.106 is directly connected, Null0
S 183.60.203.120 is directly connected, Null0
S 183.60.205.2 is directly connected, Null0
S 183.60.203.14 is directly connected, Null0
S 183.60.107.94 is directly connected, Null0
S* 0.0.0.0/0 [1/0] via 221.4.162.254
Router#
關鍵字:php、日志、機制
新文章:
- CentOS7下圖形配置網絡的方法
- CentOS 7如何添加刪除用戶
- 如何解決centos7雙系統(tǒng)后丟失windows啟動項
- CentOS單網卡如何批量添加不同IP段
- CentOS下iconv命令的介紹
- Centos7 SSH密鑰登陸及密碼密鑰雙重驗證詳解
- CentOS 7.1添加刪除用戶的方法
- CentOS查找/掃描局域網打印機IP講解
- CentOS7使用hostapd實現無AP模式的詳解
- su命令不能切換root的解決方法
- 解決VMware下CentOS7網絡重啟出錯
- 解決Centos7雙系統(tǒng)后丟失windows啟動項
- CentOS下如何避免文件覆蓋
- CentOS7和CentOS6系統(tǒng)有什么不同呢
- Centos 6.6默認iptable規(guī)則詳解