laravel如何捕获Throwable错误不让脚本停止

在单文件脚本中执行 可以捕获到错误 脚本不会停止

test.php文件内容

<?php
while (true){
    try {
        //直接执行未定义函数
        sdfs();
    }catch (Throwable $e) {
        echo  $e->getLine() . $e->getMessage();
    } catch (\Exception $e) {
        echo  $e->getLine() . $e->getMessage();
    } catch (Error $e) {
        echo  $e->getLine() . $e->getMessage();
    } finally {

    }
    sleep(3);
}

直接执行 php test.php

在laravel6.20框架 Command中执行 会导致脚本停止

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class TestCommand extends Command
{
    protected $signature = 'test';
    protected $description = 'Command description';
    public function __construct()
    {
        parent::__construct();
    }
    public function handle()
    {
        while (true){
            try {
                //直接执行未定义函数
                sdfs();
            }catch (Throwable $e) {
                //
                echo  $e->getLine() . $e->getMessage();
            } catch (\Exception $e) {
                echo  $e->getLine() . $e->getMessage();
            } catch (Error $e) {
                echo  $e->getLine() . $e->getMessage();
            } finally {

            }
            sleep(3);
        }
    }
}
  • 以上执行 php artisan test 会抛出错误并中断脚本
    Symfony\Component\Debug\Exception\FatalThrowableError : Call to undefined function App\Console\Commands\sdfs()
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
陈先生
最佳答案

catch throwable 然后继续执行你下面的逻辑 或者停止 不过在command内 会直接抛出 因为comman内没有exception handle的行为 你可以自己捕捉

3年前 评论
讨论数量: 3

尝试 catch FatalThrowableError

3年前 评论
discoverer (楼主) 3年前
陈先生

catch throwable 然后继续执行你下面的逻辑 或者停止 不过在command内 会直接抛出 因为comman内没有exception handle的行为 你可以自己捕捉

3年前 评论
use Throwable;

or

try{
}catch(\Theowable $e){
}

image

建议学习下语法或者使用phpstorm编程!

3年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!