PHP异常处理

 

伪代码如下:

try{

do somthing

if somting error

throw exception("Somting bad happened")

}catch(exception){

ouput the exception message

}

也支持创建多个处理器块来解决多个错误。为此,你可以使用各个预定义处理器,或者扩展某个预定义处理器,

创建你自己的处理器。PHP目前只提供一个简单的处理器Exception。

下面是基于多个处理块的伪代码:

try{

do IO somting

if IO somting error

throw IOexception("IO ERROR")

if Numberexception Error

throw Numberexception

}catch(IOexception){

ouput the IOexception message

}catch(Numberexception){

output the Numberexception

}

真是代码:

try{

if(!@mysql_connect('localhost','root','password')){

throw new Exception("数据库连接失败");

}else{

#OK

}

}catch(Exception $e){

echo "someError:".$e->getMessage().'发生在第'.$e->getLine().'行';

}

//下面是多处理器块的演示

try{

if(!@mysql_connect('localhost','root','password')){

throw new Exception("数据库连接失败");

}else{

#OK

if(!mysql_select_db('database'))

throw new DbException("数据库不存在");

}

}catch(Exception $e){

echo "someError:".$e->getMessage().'发生在第'.$e->getLine().'行';

}catch(DbException $e){

echo "错误发生在:".$e->getLine();

}

//DbException是Exception的扩展类。这里没有写出,因为功能与Exception一样

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值