Laravel 4 无法捕获异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14309262/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 07:29:27  来源:igfitidea点击:

Laravel 4 not able to catch Exception

exceptionexception-handlinglaravel

提问by duality_

I have tried to isolate this problem (to produce it outside my app), but I can't.

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

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

I also tried with a catch (Exception $e), the same thing happens.

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

Using this code, I get a DecryptException in the second line. How can this happen, it's in the try block?

使用此代码,我在第二行得到一个 DecryptException。这怎么会发生,它在 try 块中?

Like I said, I tried to do the same on a clean project, but there it caught the exception, so I'm asking where could I have messed something up.

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

回答by dualed

If your application is namespaced, you would need to use

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

catch(\Exception $e);
// or preferably
catch(\RuntimeException $e);

likewise, I think the DecryptExceptionyou are trying to catch is namespaced in Illuminate\Encryptionso you'd need

同样,我认为DecryptException您要捕获的名称是命名空间的,Illuminate\Encryption因此您需要

catch(\Illuminate\Encryption\DecryptException)
// or use "use" somewhere before the try/catch
use \Illuminate\Encryption\DecryptException

Keep in mindthat Laravel 4 is still alphaor pre-beta(apparently they are not sure themselves), so it is in no way stable and probably not the best choice for production.

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

回答by Abhishek Sachan

For laravel 5.1 you should write(generally at start of the file with other use statements):

对于laravel 5.1,您应该编写(通常在文件开头使用其他使用语句):

use Illuminate\Contracts\Encryption\DecryptException;

Before catch statement:

在 catch 语句之前:

try {
    $data = \Crypt::decrypt($key);
} catch (DecryptException $e) {
    echo 'caught exception';
    exit();
}

Ref: https://laravel.com/docs/5.1/encryption- under "Decrypting A Value"

参考:https: //laravel.com/docs/5.1/encryption- 在“解密值”下

May be helpful for others.

可能对其他人有帮助。