PHP 抛出异常问题

<?php


$obj = new demo;

$obj->index();

class demo{


    public function index()
    {

        try {
                $arr = [1,2,3];

                array_walk($arr, function($value,$key){

                        array_walk([2,3,5], function(){
                                //这里的闭包无法使用$this 
                                但是代码异常,没有走到catch
                                $this->test();

                        });

                        
                });


            
        } catch (Exception $e) {
              
            echo 23;
            
        }



    }


    public function test($i)
    {
        echo $i;
    }


}

上面代码异常了,但是为什么没有走catch 分支呢???

阅读 3.9k
2 个回答

这属于errortry catch只捕获异常,不处理error

try catch是处理除0属性重复这些可预见的异常情况,语法错误检查这种事情它是做不到的,也不需要它做,就算它捕捉到了你需要他做什么呢?说代码出错了吗?error这种报错需要去log里面看

PHP 7 版本可以捕获到Error的错误

PHP 5 可以用register_shutdown_function来处理错误

http://php.net/manual/en/lang...

<?php
try
{
   // Code that may throw an Exception or Error.
}
catch (Throwable $t)
{
   // Executed only in PHP 7, will not match in PHP 5
}
catch (Exception $e)
{
   // Executed only in PHP 5, will not be reached in PHP 7
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题