【PHP】PHP7的异常处理详解

来源:互联网 发布:越南新娘价格淘宝图片 编辑:程序博客网 时间:2024/04/28 20:47

前言

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 算数错误
        • DivisionByZeroError 除数为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/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将继续担任目前它抛出同样的致命错误。

Reclassify E_STRICT notices

参考资料

  • http://php.net/manual/zh/language.errors.php7.php
  • http://www.php.net/manual/en/class.arithmeticerror.php
  • https://wiki.php.net/rfc/engine_exceptions_for_php7
  • https://wiki.php.net/rfc/throwable-interface
  • https://wiki.php.net/rfc/internal_constructor_behaviour
  • https://wiki.php.net/rfc/reclassify_e_strict
0 0
原创粉丝点击