PHP学习三-PHP 7 错误处理
2018-03-31 15:58
204 查看
1.Error 层次结构
示例
2.Exception类定义
示例
发现问题: PHP7的异常通过
查看手册发现:不同于传统(PHP 5)的错误报告机制,现在大多数错误被作为 Error 异常抛出。
Exception
Error
ArithmeticError
DivisionByZeroError
AssertionError
ParseError
TypeError
示例
2.Exception类定义
示例
发现问题: PHP7的异常通过
try{}catch(){}无法抓取到。
查看手册发现:不同于传统(PHP 5)的错误报告机制,现在大多数错误被作为 Error 异常抛出。
Error类并非继承自
Exception类,所以不能用
catch (Exception $e) { ... }来捕获
Error。你可以用
catch (Error $e) { ... },或者通过注册异常处理函数(
set_exception_handler())来捕获
Error。
1.Error 层次结构
ThrowableException
Error
ArithmeticError
DivisionByZeroError
AssertionError
ParseError
TypeError
示例
#php 7.1 try { $result = eval("return 1+k;"); } catch (Throwable $e) { //此处通过Exception是无法抓取到错误的 //do sth }
2.Exception类定义
Exception { /* 属性 */ protected string $message ; protected int $code ; protected string $file ; protected int $line ; /* 方法 */ public __construct ([ string $message = "" [, int $code = 0 [, Throwable $previous = NULL ]]] ) final public string getMessage ( void ) final public Throwable getPrevious ( void ) final public int getCode ( void ) final public string getFile ( void ) final public int getLine ( void ) final public array getTrace ( void ) final public string getTraceAsString ( void ) public string __toString ( void ) final private void __clone ( void ) }
示例
抓取异常后,排查问题比较有用的方法try { } catch(Exception $e) { //查看错误抛出时的调用栈 print_r($e->getTrace()); //查看异常所在文件及行数 print_r($e->getFile().$e->getLine()); }
相关文章推荐
- PHP学习系列之错误处理和异常捕获
- PHP学习笔记——错误处理
- php学习之基础五 错误处理
- php学习笔记——错误异常处理及路径问题
- php基础学习day7(接口和错误处理)
- 【细说PHP学习】第十三章 PHP的错误和异常处理
- PHP 学习笔记(四):高级教程:Cookies,Sessions,邮件,错误处理,异常处理,过滤器
- 适用于学习的错误处理实例到工具类PHP
- php自定义错误处理和try{}catch(){}学习
- mysql远程访问 sql删除记录 伪造浏览器post php错误处理学习 django模板改tdk
- PHP学习笔记 18 - 错误处理
- php中的错误处理
- 一,php的错误处理和异常处理
- 【OpenCV学习】错误处理机制
- php学习日志(5)-解决Windows Live Writer错误:WindowsLive.Writer.CoreServices.HttpRequestHelper的类型初始值设定发生异常
- php异常、错误处理机制
- PHP如何抛出异常处理错误
- nginx源码学习——错误处理
- 1.6 PHP异常和错误处理
- [我的ASP.net学习历程]Try错误信息处理