PHP7中异常与错误处理与之前版本对比

发布时间:2019-08-07 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了PHP7中异常与错误处理与之前版本对比脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

先上代码

ECHO PHP_VERSION.PHP_EOL;
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->getMessage());
}

echo PHP_EOL."helloword".PHP_EOL;

PHP v5.6.1结果

5.6.1
PHP catchable fatal error:  argument 1 passed to add() must be an instance of int, string given, called in C:usertbshareubuntuWorkermanfortest.php on line 8 and defined in C:usertbshareubuntuWorkermanfortest.php on line 3

Catchable fatal error: argument 1 passed to add() must be an instance of int, string given, called in C:usertbshareubuntuWorkermanfortest.php on line 8 and defined in C:usertbshareubuntuWorkermanfortest.php on line 3
[Finished in 0.0s]

PHP v7.0.15结果

7.0.15-0ubuntu0.16.04.4
string(131) "Argument 1 passed to add() must be of the type integer, string given, called in /home/tb/win10share/Workerman/fortest.php on line 8"

helloword

区别

在于后者可以捕获fatal error,并且可以正常输出helloword

脚本宝典总结

以上是脚本宝典为你收集整理的PHP7中异常与错误处理与之前版本对比全部内容,希望文章能够帮你解决PHP7中异常与错误处理与之前版本对比所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。