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)文章
一個基于PDO的數(shù)據(jù)庫操作類(新) 一個PDO事務(wù)實(shí)例
原先已經(jīng)寫過一個PDO的數(shù)據(jù)庫操作類,這次只是在原先基礎(chǔ)上進(jìn)行修改。2011-07-07
PHP中spl_autoload_register()和__autoload()區(qū)別分析
這篇文章主要介紹了spl_autoload_register()和__autoload()區(qū)別,需要的朋友可以參考下2014-05-05
php生成4位數(shù)字驗(yàn)證碼的實(shí)現(xiàn)代碼
這篇文章主要介紹了php數(shù)字驗(yàn)證碼的實(shí)現(xiàn)代碼,具有一定借鑒價(jià)值,需要的朋友可以參考下2015-11-11
php 動態(tài)執(zhí)行帶有參數(shù)的類方法
PHP中,在事先知道類和類的方法名稱,使用call_user_func函數(shù)可以做動態(tài)執(zhí)行。2009-04-04
解析php擴(kuò)展php_curl.dll不加載的解決方法
本篇文章是對php擴(kuò)展php_curl.dll不加載的解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
php class中self,parent,this的區(qū)別以及實(shí)例介紹
我容易混淆public,private,protected,還容易混淆this,self這些東西。前面已經(jīng)寫了一篇關(guān)于public,private,protected 博文了,下面來說一下this,self,parent的用法2013-04-04

