php錯誤、異常處理機(jī)制(補(bǔ)充)
更新時間:2012年05月07日 20:58:39 作者:
異常處理: 意外,是在程序運(yùn)行過程中發(fā)生的意料這外的事,使用異常改變腳本正常流程
一、錯誤處理
異常處理: 意外,是在程序運(yùn)行過程中發(fā)生的意料這外的事,使用異常改變腳本正常流程
PHP5中的一個新的重要特性
if(){
}else{
}
try {
}catch(異常對象){
}
1. 如果try中代碼沒有問題,則將try中代碼執(zhí)行完后就到catch后執(zhí)行
2. 如果try中代碼有異常發(fā)生,則拋出一個異常對象(使用throw),拋出給了catch中的參數(shù), 則在try中代碼就不會再繼續(xù)執(zhí)行下去
直接跳轉(zhuǎn)到catch中去執(zhí)行, catch中執(zhí)行完成, 再繼續(xù)向下執(zhí)行
注意: 提示發(fā)生了什么異常,這不是主要我們要做事,需要在catch中解決這個異常, 如果解決不了,則出去給用戶
二、自己定義一個異常類
作用:就是寫一個或多個方法解決當(dāng)發(fā)生這個異常時的處理方式
1. 自己定義異常類,必須是Exception(內(nèi)置類)的子類,
2. Exception類中的只有構(gòu)造方法和toString()可以重寫, 其它都final
三、處理多個異常
自己定義功能類時如果在方法中拋出異常
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("文件打開失敗");
}
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<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)行過程中發(fā)生的意料這外的事,使用異常改變腳本正常流程
PHP5中的一個新的重要特性
復(fù)制代碼 代碼如下:
if(){
}else{
}
try {
}catch(異常對象){
}
1. 如果try中代碼沒有問題,則將try中代碼執(zhí)行完后就到catch后執(zhí)行
2. 如果try中代碼有異常發(fā)生,則拋出一個異常對象(使用throw),拋出給了catch中的參數(shù), 則在try中代碼就不會再繼續(xù)執(zhí)行下去
直接跳轉(zhuǎn)到catch中去執(zhí)行, catch中執(zhí)行完成, 再繼續(xù)向下執(zhí)行
注意: 提示發(fā)生了什么異常,這不是主要我們要做事,需要在catch中解決這個異常, 如果解決不了,則出去給用戶
二、自己定義一個異常類
作用:就是寫一個或多個方法解決當(dāng)發(fā)生這個異常時的處理方式
1. 自己定義異常類,必須是Exception(內(nèi)置類)的子類,
2. Exception類中的只有構(gòu)造方法和toString()可以重寫, 其它都final
三、處理多個異常
自己定義功能類時如果在方法中拋出異常
復(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("文件打開失敗");
}
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<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ù)某個字段進(jìn)行水平合并,橫向合并案例分析
這篇文章主要介紹了PHP實(shí)現(xiàn)數(shù)組根據(jù)某個字段進(jìn)行水平合并,橫向合并,結(jié)合具體案例形式分析了php數(shù)組遍歷、合并等相關(guān)操作技巧,需要的朋友可以參考下2019-10-10php 使用curl模擬ip和來源進(jìn)行訪問的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄猵hp 使用curl模擬ip和來源進(jìn)行訪問的實(shí)現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05php連接oracle數(shù)據(jù)庫的方法(測試成功)
這篇文章主要介紹了php連接oracle數(shù)據(jù)庫的方法,簡單分析了php連接Oracle數(shù)據(jù)庫的常見方法與具體操作技巧,并對可能出現(xiàn)的問題進(jìn)行了總結(jié)分析,需要的朋友可以參考下2016-05-05PHP基于rabbitmq操作類的生產(chǎn)者和消費(fèi)者功能示例
這篇文章主要介紹了PHP基于rabbitmq操作類的生產(chǎn)者和消費(fèi)者功能,結(jié)合實(shí)例形式分析了基于rabbitmq操作類的生產(chǎn)者和消費(fèi)者定義與使用方法,需要的朋友可以參考下2018-06-06刪除html標(biāo)簽得到純文本可處理嵌套的標(biāo)簽
這篇文章主要介紹了通過刪除html標(biāo)簽得到的純文本可處理嵌套的標(biāo)簽,需要的朋友可以參考下2014-04-04php 過濾英文標(biāo)點(diǎn)符號及過濾中文標(biāo)點(diǎn)符號代碼
這篇文章主要介紹了php過濾英文標(biāo)點(diǎn)符號及過濾中文標(biāo)點(diǎn)符號的方法,需要的朋友可以參考下2014-06-06