您好,欢迎访问一九零五行业门户网

PHP 7 错误异常级别

这篇文章介绍的内容是关于php7错误异常级别,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下
php 7 错误异常级别
探究 php 7 的异常层次结构
在过去的 php 中,几乎不可能会去处理致命错误。致命错误只会轻易的终止脚本执行,而不会调用 set_error_hander() 错误处理程序。
在 php 7 中,当致命或是可恢复性错误 (e_error and e_recoverable_error) 发生时,异常会被捕获,而不是中止脚本。在特定的情况下,还是存在会致命的错误,比如内存不足之类,也会像之前一样立即中止脚本。php 7 中未捕获的异常依旧时致命错误。这意味着,如果 php 5.x 中未捕获的异常,在 php 7 中依旧是致命错误。
注意,例如警告或是通知错误在 php 7 中保持不变,只有致命错误或是可恢复性错误会抛出异常。
致命或是可恢复性错误的抛出并不延伸自 exception 类。这种分离是为了防止现存的 php 5.x 代码接收到的错误异常调用到终止程序。致命或是可恢复错误抛出的异常将实例化一个新的异常类:error。和其他异常类相同,被捕获到的 error 类将会在最后一个程序块执行完毕之后再行处理。
相较于 php 7 alpha-2 之前,php 7 的异常类层次有所不同, 被抛出的致命和可恢复性的错误将于 engineexception 类实例化,而 enginexception 类并不继承于 exception。exception 和 engineexception 都继承于 baseexception。
throwable为了联合这两个异常分支,exception 和 error 都实现了一个新的接口,throwable。
php 7 中新的异常层次如下:
throwable //(接口) |- exception implements throwable |- ... |- error implements throwable |- typeerror extends error |- parseerror extends error // 编译时错误 |- arithmeticerror extends error |- pisionbyzeroerror extends arithmeticerror |- assertionerror extends error
如果在 php 7 中定义 throwable 接口,那应该和下面的代码相近。
interface throwable{ public function getmessage(): string; public function getcode(): int; public function getfile(): string; public function getline(): int; public function gettrace(): array; public function gettraceasstring(): string; public function getprevious(): throwable; public function __tostring(): string;}
这个接口应当很熟悉。throwable 特定的方法和 exception 的相同。唯一不同的是 throwable::getprevious() 会返回 throwable exception 和 error 类的构造函数都将接收一个 throwable 的实例作为先前的异常。
throwable 可以在 try/catch 块中用老捕获异常或是错误对象(将来可能可以捕获更多的异常类型)。记住,这里更建议捕获更为具体的异常类,并采取相应的处理措施。然而,在一些场合下,需要宽泛的捕获异常(比如日志或是框架的错误处理)。在 php 7 中,这些异常捕获块更适合使用 throwable 而不是 exception。
try { // code that may throw an exception or error.} catch (throwable $t) { // handle exception}
自定义类不能实现 throwable 插件,这个部分由于可预见性和一致性:只有实例化 excetion 和 error 类才能抛出异常。此外,异常携带了栈中被创建的对象的信息。自定义类并未自动拥有保存信息的参数。
throwable can be extended to create package-specific interfaces or add additional methods. 只有继承了 exception 或是 error 的类才可以实现拓展了 throwable 的插件。
interface mypackagethrowable extends throwable {}class mypackageexception extends exception implements mypackagethrowable {}throw new mypackageexception();
error在 php 5.下版本中所有的 errors 都是致命错误或是可恢复性致命错误,而在 php 7 中都抛出 error 的实例化。像其他异常 error 对象可以通过 try/catch 程序块捕获。
$var = 1;try { $var->method(); // throws an error object in php 7.} catch (error $e) { // handle error}
通常,之前的致命错误都将抛出 error 基类的实例化,但是一些错误会抛出更加确切的 error 子类:typeerror, parseerror, and assertionerror。
typeerror (类型错误)typeerror 实例化的抛出是由实参和形参 当调用函数时申明的形参和实参类型不一致(传入参数和方法中定义的参数类型不一致)将会抛出一个 typeerror 实例。
function add(int $left, int $right){ return $left + $right; }try { $value = add('left', 'right'); } catch (typeerror $e) { echo $e->getmessage(), "\n"; }
得到的输出:
argument 1 passed to add() must be of the type integer, string given
parseerror (解析错误)included/required 文件,或者 eval() 中的代码包含语法错误时,parseerror 将会被抛出。
try { require 'file-with-parse-error.php'; } catch (parseerror $e) { echo $e->getmessage(), "\n"; }
arithmeticerror (算数错误)抛出 arithmeticerror 错误有两种情况:负数位移,或者使用 php_int_min 当作分子,-1 做分母调用 intp()(php_ini_min / -1 返回值是浮点型)。
try { $value = 1 << -1; } catch (arithmeticerror $e) { echo $e->getmessage(), "\n"; }
pisionbyzeroerror (分母为零)分母为零时使用 intp() 或者取余(%) 会抛出 pisionbyzeroerror 错误。注意,除零只会引起一个警告,计算结果为 nan。
try { $value = 1 % 0; } catch (pisionbyzeroerror $e) { echo $e->getmessage(), "\n"; }
assertionerror (断言)当不满足 assert() 设定的条件时,将会抛出一个 assertionerror 错误。
ini_set('zend.assertions', 1); ini_set('assert.exception', 1);$test = 1; assert($test === 0);
不满足 assert() 设定的条件,抛出一个 assertionerror 错误,并且 assert.exception = 1,异常输出如下:
fatal error: uncaught assertionerror: assert($test === 0)
assert() is only executed and will only throw an assertionerror if assertions are enabled and set to throw exceptions with ini settings zend.assertions = 1 and assert.exception = 1.
使用 error用户可以创建自己的 error 类,作为 error 基类的拓展。这可能带来重要的问题:什么场合下应该抛出一个 exception 类的子类实例,什么场合下又应该抛出 error 类的子类实例?
由于错误对象不应当在程序运行中处理,捕获错误对象应当是少见的。通常而言,错误对象应当捕获并记录之,执行必要的清理,并给用户展示错误信息。
编写兼容 php 5.x 和 7 exceptions 类的代码在 php 5.x 和 7 使用相同的代码捕获异常,可以实用多重捕获代码块,首先捕获 throwable,之后时 exception。一旦不需要维护 php 5.x 的系统,代码块可以立刻被清理掉。
try { // code that may throw an exception or error.} catch (throwable $t) { // executed only in php 7, will not match in php 5.x} catch (exception $e) { // executed only in php 5.x, will not be reached in php 7}
英文原文: trowski.com/2015
本文虽拙,却也系作者劳动,转载还请保留本文链接: http://cyleft.com/?p=721
相关推荐:
php错误级别详解
php错误与异常调试视频教程资源分享
以上就是php 7 错误异常级别的详细内容。
其它类似信息

推荐信息