Laravel 4无法捕捉异常

dua*_*ty_ 8 exception-handling exception laravel

我试图隔离这个问题(在我的应用程序之外生成它),但我不能.

try {
    $has_cache = Cache::has($cache_key);
}
catch (DecryptException $e) {
    echo "No biggie";
    exit;
}
Run Code Online (Sandbox Code Playgroud)

我也尝试过catch (Exception $e),同样的事情发生了.

使用此代码,我在第二行得到DecryptException.怎么会发生这种情况,它在try块中?

就像我说的那样,我试图在一个干净的项目上做同样的事情,但它抓住了异常,所以我问我在哪里搞砸了什么.

dua*_*led 24

如果您的应用程序是命名空间,则需要使用

catch(\Exception $e);
// or preferably
catch(\RuntimeException $e);
Run Code Online (Sandbox Code Playgroud)

同样,我认为DecryptException你要抓住的是命名空间,Illuminate\Encryption所以你需要

catch(\Illuminate\Encryption\DecryptException)
// or use "use" somewhere before the try/catch
use \Illuminate\Encryption\DecryptException
Run Code Online (Sandbox Code Playgroud)

请记住,Laravel 4仍然是alphapre-beta(显然他们不确定自己),所以它绝不是稳定的,可能不是生产的最佳选择.