PHP7的异常处理详解

来源:文书网 2.97W

在PHP中碰到异常的时候应该如何处理呢,就跟随本站小编一起去了解下吧,想了解更多相关信息请持续关注我们应届毕业生考试网!

PHP7的异常处理详解

前言

PHP7的改动中,影响比较大的,包括异常处理。

概述

更多的异常是直接通过PHP直接处理的,和之前的PHP5不同的是更多的异常是通过Error exceptions来抛出。

作为一个普通的扩展,Error exceptions会持续冒出直到匹配到对应的catch块。如果没有进行匹配,就会触发被设置的set_exception_handler()来执行处理,如果没有默认的异常处理程序,则该异常将被转换为一个致命错误,并且将被像一个传统的错误被处理。

由于Error在错误层次结构不继承异常,像这样的代码catch (Exception $e) { ... }在PHP5中并不会捕获到对应的.异常。我们可以用代码catch (Error $e) { ... }或者 set_exception_handler(),来对Error进行处理。

错误的层级结构

Throwable

Error 错误

ArithmeticError 算数错误

PisionByZeroError 除数为0的错误

AssertionError 声明错误

ParseError 解析错误

TypeError 类型错误

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/ on line 14" ["string":"Error":private]=> string(0) "" ["code":protected]=> int(0) ["file":protected]=> string(48) "/Applications/mamp/apache2/htdocs/curl/" ["line":protected]=> int(9) ["trace":"Error":private]=> array(1) { [0]=> array(4) { ["file"]=> string(48) "/Applications/mamp/apache2/htdocs/curl/" ["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将继续担任目前它抛出同样的致命错误。

热门标签