laravel 为什么 `catch (Exception $e)` 不处理这个 `ErrorException`?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15071191/
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:35:10  来源:igfitidea点击:

Why does `catch (Exception $e)` not handle this `ErrorException`?

phplaravellaravel-4

提问by duality_

I get the ErrorExceptionon the function call bellow. How can this be? Why is it not caught?

我得到了ErrorException下面的函数调用。怎么会这样?为什么没有被抓到?

try {
    static::$function_name($url);
}
catch (Exception $e) {}

The underlying reason for the error is a file_put_contentscall. I'm using the Laravel 4 framework, if it makes any difference.

错误的根本原因是file_put_contents调用。我正在使用 Laravel 4 框架,如果它有什么不同的话。

回答by pete otaqui

I suspect that you need to write this:

我怀疑你需要这样写:

try {
    static::$function_name($url);
} catch (\Exception $e) {}

Note the \ in front of Exception.

注意 Exception 前面的 \。

When you have declared a namespace, you need to specify the root namespace in front of classes like Exception, otherwise the catch block here will be looking for \Your\Namespace\Exception, and not just \Exception

声明了命名空间后,需要在Exception之类的类前面指定根命名空间,否则这里的catch块会寻找\Your\Namespace\Exception,而不仅仅是\Exception