详细介绍PHP7的异常处理代码示例

黄舟
黄舟 原创
2023-03-06 14:10:02 977浏览

前言

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

概述

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

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

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

错误的层级结构

  • Throwable

    • ….

    • ArithmeticError 算数错误

    • AssertionError 声明错误

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

Reclassify E_STRICT notices


以上就是详细介绍PHP7的异常处理代码示例的详细内容,更多请关注php中文网其它相关文章!

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。