如何在 Laravel 中异常后继续执行


How to continue execution after exception in Laravel?

嗨,有一个从CSV文件导入新用户的函数。如果用户的密码字段为空,我得到一个Cartalyst '' 哨兵 '' 用户 '' 密码必填异常

我正在尝试捕获此异常并将消息存储到会话变量中,但我想在异常发生后继续导入其余用户。

目前我正在使用这个处理程序:

App::error(function(Cartalyst'Sentry'Users'PasswordRequiredException $exception)
{
    Session::push('import.errors', $exception->getMessage());
});

使用 XDebug,我确定处理程序被触发 - 但执行也会在错误后停止。

如何继续导入 CSV 中的其余用户?

您需要捕获函数导入新用户的异常,然后可以选择忽略它:

your_import_function($users_to_be_imported)
{
     foreach ($users_to_be_imported as $user) {
         try {
             // Import user code here
         } catch (Cartalyst'Sentry'Users'PasswordRequiredException $exception) {
             // Log user that failed here
         }
     }
 }

因为您正在捕获函数中的异常 - 它不应该"冒泡"到App::error()函数