php錯(cuò)誤處理和異常處理機(jī)制
添加時(shí)間:2014-7-4 3:17:21
添加:
思海網(wǎng)絡(luò)
當(dāng)我們開(kāi)發(fā)程序時(shí),有時(shí)候程序出現(xiàn)了問(wèn)題,我們就可以用以下幾種辦法找出錯(cuò)誤。
開(kāi)發(fā)階段:開(kāi)發(fā)時(shí)輸出所有的錯(cuò)誤報(bào)告,有利于我們進(jìn)行程序調(diào)試
運(yùn)行階段:我們不要讓程序輸出任何一種錯(cuò)誤報(bào)告(不能讓用戶看到(包括懂技術(shù), 不懂技術(shù)的人))
將錯(cuò)誤報(bào)告寫入日志中
一、指定錯(cuò)誤報(bào)告 error_reporting = E_LL
二、關(guān)閉錯(cuò)誤輸出 display_errors = Off
三、開(kāi)啟錯(cuò)誤日志功能 log_errors = On
1. 默認(rèn)如果不指定錯(cuò)誤日志位置,則默認(rèn)寫WEB服務(wù)器的日志中
2. 為error_log選項(xiàng)指定 一個(gè)文件名(可寫)
3. 寫入到操作系統(tǒng)日志中error_log=syslog
以下代碼示例
";
?>
當(dāng)然php還提供了函數(shù)error_get_last()來(lái)獲得錯(cuò)誤信息
函數(shù)定義和用法
error_get_last()函數(shù)獲取最后發(fā)生的錯(cuò)誤。 該函數(shù)以數(shù)組的形式返回最后發(fā)生的錯(cuò)誤。 返回的數(shù)組包含 4 個(gè)鍵和值: [type] - 錯(cuò)誤類型 [message] - 錯(cuò)誤消息 [file] - 發(fā)生錯(cuò)誤所在的文件 [line] - 發(fā)生錯(cuò)誤所在的
小例子:
輸出:
Array ( [type] => 8 [message] => Undefined variable: test [file] => D:\www\test.php [line] => 2 )
所以這樣我們也很方便了。。。這樣是不是對(duì)調(diào)試程序和排查錯(cuò)誤的時(shí)候很有幫助呢?
這些錯(cuò)誤報(bào)告級(jí)別是錯(cuò)誤處理程序旨在處理的錯(cuò)誤的不同的類型:
值 常量 描述
2 E_WARNING 非致命的 run-time 錯(cuò)誤。不暫停腳本執(zhí)行。
8 E_NOTICE
Run-time 通知。
腳本發(fā)現(xiàn)可能有錯(cuò)誤發(fā)生,但也可能在腳本正常運(yùn)行時(shí)發(fā)生。
256 E_USER_ERROR 致命的用戶生成的錯(cuò)誤。這類似于程序員使用 PHP 函數(shù) trigger_error() 設(shè)置的 E_ERROR。
512 E_USER_WARNING 非致命的用戶生成的警告。這類似于程序員使用 PHP 函數(shù) trigger_error() 設(shè)置的 E_WARNING。
1024 E_USER_NOTICE 用戶生成的通知。這類似于程序員使用 PHP 函數(shù) trigger_error() 設(shè)置的 E_NOTICE。
4096 E_RECOVERABLE_ERROR 可捕獲的致命錯(cuò)誤。類似 E_ERROR,但可被用戶定義的處理程序捕獲。(參見(jiàn) set_error_handler())
8191 E_ALL
所有錯(cuò)誤和警告,除級(jí)別 E_STRICT 以外。
(在 PHP 6.0,E_STRICT 是 E_ALL 的一部分)
php異常處理機(jī)制
定義:
異常處理: 意外,是在程序運(yùn)行過(guò)程中發(fā)生的意料這外的事,使用異常改變腳本正常流程
語(yǔ)法格式:
try
{ //...}
catch(Exception $e)
{ //...}
PHP中try{}catch{}是異常處理.
將要執(zhí)行的代碼放入TRY塊中,如果這些代碼執(zhí)行過(guò)程中某一條語(yǔ)句發(fā)生異常,則程序直接跳轉(zhuǎn)到CATCH塊中,由$e收集錯(cuò)誤信息和顯示.
PHP中try{}catch{}語(yǔ)句
為了進(jìn)一步處理異常,我們需要使用PHP中try{}catch{}----包括Try語(yǔ)句和至少一個(gè)的catch語(yǔ)句。任何調(diào)用 可能拋出異常的方法的代碼都應(yīng)該使用try語(yǔ)句。Catch語(yǔ)句用來(lái)處理可能拋出的異常。
例子:
我寫一段代碼:
自己定義一個(gè)異常類
作用:就是寫一個(gè)或多個(gè)方法解決當(dāng)發(fā)生這個(gè)異常時(shí)的處理方式
1. 自己定義異常類,必須是Exception(內(nèi)置類)的子類, 可以查看PHP手冊(cè)里面Exception(內(nèi)置類)的使用方法
2. Exception類中的只有構(gòu)造方法和toString()可以重寫, 其它都final
";
}
function open(){
touch("tmp.txt");
$file=fopen("tmp.txt", "r");
return $file;
}
}
?>
1. 如果try中代碼沒(méi)有問(wèn)題,則將try中代碼執(zhí)行完后就到catch后執(zhí)行
2. 如果try中代碼有異常發(fā)生,則拋出一個(gè)異常對(duì)象(使用throw),拋出給了catch中的參數(shù), 則在try中代碼就不會(huì)再繼續(xù)執(zhí)行下去 直接跳轉(zhuǎn)到catch中去執(zhí)行, catch中執(zhí)行完成, 再繼續(xù)向下執(zhí)行
注意: 提示發(fā)生了什么異常,這不是主要我們要做事,需要在catch中解決這個(gè)異常, 如果解決不了,則出去給用戶在下面代碼中,如果我沒(méi)有這個(gè)TMP.TXT文件的話,就會(huì)拋出異常了。
如果有異常,我們調(diào)用OPEN方法就可以解決這個(gè)異常了。
getMessage()."
"; //getMessage() 是PHP里面內(nèi)置的方法,可以直接調(diào)用
$file=$e->open();
}
下面將代碼進(jìn)行整理以及多個(gè)異常處理方法:
";
}
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("文件打開(kāi)失敗");
}
function demo($num=0){
if($num==1)
throw new DemoException("演示出異常");
}
function test($num=0){
if($num==1)
throw new TestException("測(cè)試出錯(cuò)");
}
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#
關(guān)鍵字:php、日志、機(jī)制
新文章:
- CentOS7下圖形配置網(wǎng)絡(luò)的方法
- CentOS 7如何添加刪除用戶
- 如何解決centos7雙系統(tǒng)后丟失windows啟動(dòng)項(xiàng)
- CentOS單網(wǎng)卡如何批量添加不同IP段
- CentOS下iconv命令的介紹
- Centos7 SSH密鑰登陸及密碼密鑰雙重驗(yàn)證詳解
- CentOS 7.1添加刪除用戶的方法
- CentOS查找/掃描局域網(wǎng)打印機(jī)IP講解
- CentOS7使用hostapd實(shí)現(xiàn)無(wú)AP模式的詳解
- su命令不能切換root的解決方法
- 解決VMware下CentOS7網(wǎng)絡(luò)重啟出錯(cuò)
- 解決Centos7雙系統(tǒng)后丟失windows啟動(dòng)項(xiàng)
- CentOS下如何避免文件覆蓋
- CentOS7和CentOS6系統(tǒng)有什么不同呢
- Centos 6.6默認(rèn)iptable規(guī)則詳解