为什么`catch(Exception $ e)`不处理`ErrorException`吗? [英] Why does `catch (Exception $e)` not handle this `ErrorException`?
问题描述
我在下面的函数调用中得到了ErrorException
.怎么会这样?为什么没有抓住?
I get the ErrorException
on the function call bellow. How can this be? Why is it not caught?
try {
static::$function_name($url);
}
catch (Exception $e) {}
该错误的根本原因是file_put_contents
调用.如果有任何区别,我正在使用Laravel 4框架.
The underlying reason for the error is a file_put_contents
call. I'm using the Laravel 4 framework, if it makes any difference.
推荐答案
我怀疑您需要这样写:
try {
static::$function_name($url);
} catch (\Exception $e) {}
请注意Exception前面的\.
Note the \ in front of Exception.
声明了名称空间后,需要在Exception之类的类前面指定根名称空间,否则此处的catch块将查找\Your\Namespace\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
这篇关于为什么`catch(Exception $ e)`不处理`ErrorException`吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!