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()
推荐文章: