基本异常处理

  1. 基本格式:
try{
    /* code block*/
} catch(Exception $e){  # $e是异常类的实例对象
    /* handle code */
} finally{
    /* finally code */
}
  1. 手动引发异常:
throw new Exception($message, $code);
  1. Exception Class method:
getCode()       // Returns the code as passed to the constructor
getMessage()    // Returns the message as passed to the constructor
getFile()       //Returns the full path to the code file where the exception was raised
getLine()       //Returns the line number in the code file where the exception was raised
getTrace()      //Returns an array containing a backtrace where the exception was raised
getTraceAsString()  //Returns the same information as getTrace, formatted as a string
getPrevious()       //Returns the previous exception (from the third parameter to the constructor)
__toString()        //Allows you to simply echo an  Exception object, giving all the information from the above methods, 即直接以字符串的形式显示$e,等同于echo $e
  1. 用户自定义的异常:
    Exception基类中除了__toString()是公有方法外,其它方法均为final,所以用户只能修改__toString()方法来更改显示样式及异常类的名称。 # 通过不同的异常类可以方便地排查错误

  2. 用错误抑制符@修饰的代码在发生错误时无论是否引发异常(取决于php.ini的配置)都会产生警告。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值