php錯(cuò)誤、異常處理機(jī)制(補(bǔ)充)
更新時(shí)間:2012年05月07日 20:58:39 作者:
異常處理: 意外,是在程序運(yùn)行過(guò)程中發(fā)生的意料這外的事,使用異常改變腳本正常流程
一、錯(cuò)誤處理
異常處理: 意外,是在程序運(yùn)行過(guò)程中發(fā)生的意料這外的事,使用異常改變腳本正常流程
PHP5中的一個(gè)新的重要特性
if(){
}else{
}
try {
}catch(異常對(duì)象){
}
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è)異常, 如果解決不了,則出去給用戶(hù)
二、自己定義一個(gè)異常類(lèi)
作用:就是寫(xiě)一個(gè)或多個(gè)方法解決當(dāng)發(fā)生這個(gè)異常時(shí)的處理方式
1. 自己定義異常類(lèi),必須是Exception(內(nèi)置類(lèi))的子類(lèi),
2. Exception類(lèi)中的只有構(gòu)造方法和toString()可以重寫(xiě), 其它都final
三、處理多個(gè)異常
自己定義功能類(lèi)時(shí)如果在方法中拋出異常
class OpenFileException extends Exception {
function __construct($message = null, $code = 0){
parent::__construct($message, $code);
echo "wwwwwwwwwwwwwww<br>";
}
function open(){
touch("tmp.txt");
$file=fopen("tmp.txt", "r");
return $file;
}
}
class DemoException extends Exception {
function pro(){
echo "處理demo發(fā)生的異常<br>";
}
}
class TestException extends Exception {
function pro(){
echo "這里處理test發(fā)生的異常<br>";
}
}
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<br>";
$my=new MyClass();
$my->openfile();
$my->demo(0);
$my->test(0);
$my->fun(1);
echo "22222222222222222<br>";
}catch(OpenFileException $e){ //$e =new Exception();
echo $e->getMessage()."<br>";
$file=$e->open();
}catch(DemoException $e){
echo $e->getMessage()."<br>";
$e->pro();
}catch(TestException $e){
echo $e->getMessage()."<br>";
$e->pro();
}catch(Exception $e){
echo $e->getMessage()."<br>";
}
var_dump($file);
echo "444444444444444444444<br>";
異常處理: 意外,是在程序運(yùn)行過(guò)程中發(fā)生的意料這外的事,使用異常改變腳本正常流程
PHP5中的一個(gè)新的重要特性
復(fù)制代碼 代碼如下:
if(){
}else{
}
try {
}catch(異常對(duì)象){
}
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è)異常, 如果解決不了,則出去給用戶(hù)
二、自己定義一個(gè)異常類(lèi)
作用:就是寫(xiě)一個(gè)或多個(gè)方法解決當(dāng)發(fā)生這個(gè)異常時(shí)的處理方式
1. 自己定義異常類(lèi),必須是Exception(內(nèi)置類(lèi))的子類(lèi),
2. Exception類(lèi)中的只有構(gòu)造方法和toString()可以重寫(xiě), 其它都final
三、處理多個(gè)異常
自己定義功能類(lèi)時(shí)如果在方法中拋出異常
復(fù)制代碼 代碼如下:
class OpenFileException extends Exception {
function __construct($message = null, $code = 0){
parent::__construct($message, $code);
echo "wwwwwwwwwwwwwww<br>";
}
function open(){
touch("tmp.txt");
$file=fopen("tmp.txt", "r");
return $file;
}
}
class DemoException extends Exception {
function pro(){
echo "處理demo發(fā)生的異常<br>";
}
}
class TestException extends Exception {
function pro(){
echo "這里處理test發(fā)生的異常<br>";
}
}
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<br>";
$my=new MyClass();
$my->openfile();
$my->demo(0);
$my->test(0);
$my->fun(1);
echo "22222222222222222<br>";
}catch(OpenFileException $e){ //$e =new Exception();
echo $e->getMessage()."<br>";
$file=$e->open();
}catch(DemoException $e){
echo $e->getMessage()."<br>";
$e->pro();
}catch(TestException $e){
echo $e->getMessage()."<br>";
$e->pro();
}catch(Exception $e){
echo $e->getMessage()."<br>";
}
var_dump($file);
echo "444444444444444444444<br>";
相關(guān)文章
PHP實(shí)現(xiàn)數(shù)組根據(jù)某個(gè)字段進(jìn)行水平合并,橫向合并案例分析
這篇文章主要介紹了PHP實(shí)現(xiàn)數(shù)組根據(jù)某個(gè)字段進(jìn)行水平合并,橫向合并,結(jié)合具體案例形式分析了php數(shù)組遍歷、合并等相關(guān)操作技巧,需要的朋友可以參考下2019-10-10php 使用curl模擬ip和來(lái)源進(jìn)行訪問(wèn)的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇php 使用curl模擬ip和來(lái)源進(jìn)行訪問(wèn)的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05php連接oracle數(shù)據(jù)庫(kù)的方法(測(cè)試成功)
這篇文章主要介紹了php連接oracle數(shù)據(jù)庫(kù)的方法,簡(jiǎn)單分析了php連接Oracle數(shù)據(jù)庫(kù)的常見(jiàn)方法與具體操作技巧,并對(duì)可能出現(xiàn)的問(wèn)題進(jìn)行了總結(jié)分析,需要的朋友可以參考下2016-05-05PHP基于rabbitmq操作類(lèi)的生產(chǎn)者和消費(fèi)者功能示例
這篇文章主要介紹了PHP基于rabbitmq操作類(lèi)的生產(chǎn)者和消費(fèi)者功能,結(jié)合實(shí)例形式分析了基于rabbitmq操作類(lèi)的生產(chǎn)者和消費(fèi)者定義與使用方法,需要的朋友可以參考下2018-06-06刪除html標(biāo)簽得到純文本可處理嵌套的標(biāo)簽
這篇文章主要介紹了通過(guò)刪除html標(biāo)簽得到的純文本可處理嵌套的標(biāo)簽,需要的朋友可以參考下2014-04-04php 過(guò)濾英文標(biāo)點(diǎn)符號(hào)及過(guò)濾中文標(biāo)點(diǎn)符號(hào)代碼
這篇文章主要介紹了php過(guò)濾英文標(biāo)點(diǎn)符號(hào)及過(guò)濾中文標(biāo)點(diǎn)符號(hào)的方法,需要的朋友可以參考下2014-06-06