前言
PHP7的改动中,影响比较大的,包括异常处理。
概述
更多的异常是直接通过PHP直接处理的,和之前的PHP5不同的是更多的异常是通过Error exceptions来抛出。
作为一个普通的扩展,Error exceptions会持续冒出直到匹配到对应的catch块。如果没有进行匹配,就会触发被设置的set_exception_handler()来执行处理,如果没有默认的异常处理程序,则该异常将被转换为一个致命错误,并且将被像一个传统的错误被处理。
由于Error在错误层次结构不继承异常,像这样的代码catch (Exception $e) { ... }在PHP5中并不会捕获到对应的异常。我们可以用代码catch (Error $e) { ... }或者 set_exception_handler(),来对Error进行处理。
立即学习“PHP免费学习笔记(深入)”;
错误的层级结构
Throwable
….
ArithmeticError 算数错误
AssertionError 声明错误
店小二个人网店系统无限制版下载v3.0修正版更新:新增加的功能:1、网店logo在线上传。2、添加图片上传预览功能。3、增加ICP备案字段,在线添加。4、添加管理员管理功能。5、添加送货详细设置功能。6、增加客户端验证功能。7、增加5种样式。修正的地方:1、订单不能删除。2、产品图片不能删除。3、管理员不能修改密码。4、小数显示不正常,比如0.68显示为.685、退出不方便。6、前台热门商品文字显示不换行。7、商品详细介绍页面
ParseError 解析错误
TypeError 类型错误
pisionByZeroError 除数为0的错误
Error 错误
Exception 异常
PHP RFC
Throwable Interface
function add(int $left, int $right) {
return $left + $right;
}try { echo add('left', 'right');
} catch (Exception $e) { // Handle exception} catch (Error $e) { // Clearly a different type of object
// Log error and end gracefully
var_dump($e);
}这里,并没有出现服务器500的错误。原因在于,PHP7中的Error把它拦截住了,没有冒泡在服务器中。
object(TypeError)#1 (7) {
["message":protected]=>
string(139) "Argument 1 passed to add() must be of the type integer, string given, called in /Applications/mamp/apache2/htdocs/curl/error.php on line 14"
["string":"Error":private]=> string(0) ""
["code":protected]=> int(0)
["file":protected]=> string(48) "/Applications/mamp/apache2/htdocs/curl/error.php"
["line":protected]=> int(9)
["trace":"Error":private]=>
array(1) {
[0]=>
array(4) {
["file"]=> string(48) "/Applications/mamp/apache2/htdocs/curl/error.php"
["line"]=> int(14)
["function"]=> string(3) "add"
["args"]=>
array(2) {
[0]=> string(4) "left"
[1]=> string(5) "right"
}
}
}
["previous":"Error":private]=>
NULL
}这样我们就可以通过日志的方式记录他们。
Exceptions in the engine (for PHP 7)
function call_method($obj) {
$obj->method();
}try {
call_method(null);
// oops!
}
catch (EngineException $e) {
echo "Exception: {$e->getMessage()}\n";
}//其实上面的例子我在运行过程中,并没有被EngineException捕获异常,经过测试,也是通过Error进行的错误的拦截如果异常没有被捕获,PHP将继续担任目前它抛出同样的致命错误。










