Laravel异常后如何继续执行?

How to continue execution after exception in Laravel?

你好有一个从 CSV 文件导入新用户的功能。 如果用户的密码字段为空,我会得到一个 Cartalyst\Sentry\Users\PasswordRequiredException

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

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

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
         }
     }
 }

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