PHP“未找到异常”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10000539/
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
PHP "Exception not found"
提问by didi_X8
I have a somehow funny issue. While trying to understand why a certain website returns http code 500 to browser, I found the message
我有一个有趣的问题。在试图理解为什么某个网站向浏览器返回 http 代码 500 时,我发现了这条消息
PHP Fatal error: Class 'MZ\MailChimpBundle\Services\Exception' not found in /var/www/website/vendor/bundles/MZ/MailChimpBundle/Services/MailChimp.php on line 41
in apache log. Looking at the mentioned line:
在 apache 日志中。查看提到的行:
throw new Exception('This bundle needs the cURL PHP extension.');
I now understand how to get the site working, but I still wonder why the code for throwing the exception (which would have resulted in a more helpful log message) failed. What could be the reason?
我现在了解如何让网站正常工作,但我仍然想知道为什么抛出异常的代码(这会导致更有用的日志消息)失败。可能是什么原因?
回答by hakre
The MZMailChimpBundledoes not contain a class named Exceptionwithin the MZ\MailChimpBundle\Servicesnamespace.
该MZMailChimpBundle不包含名为类Exception的内部MZ\MailChimpBundle\Services命名空间。
Because of that simple fact and as the error message that the exception should signal is related to an integration problem (check for the curl library) I assume that this is a bug.
由于这个简单的事实以及异常应该发出信号的错误消息与集成问题(检查 curl 库)有关,我认为这是一个错误。
The original has meant \Exceptionand not Exceptionhere. It's a somewhat common mistake that can happen with namespaces. To fix the file, either alias/import \Exceptionas Exception:
原文的意思是\Exception而不是Exception在这里。这是命名空间可能发生的一个有点常见的错误。要修复文件,别名/导入\Exception为Exception:
namespace MZ\MailChimpBundle\Services;
use Exception;
and/or change the newline in MZMailChimpBundle/Services/MailChimp.php:
和/或更改new行MZMailChimpBundle/Services/MailChimp.php:
throw new \Exception('This bundle needs the cURL PHP extension.');
See as well the related question: How to use “root” namespace of php?and the one with the same Class 'Namespace\Example' not founderror message: Calling a static method from a class in another namespace in PHP.
另见相关问题:如何使用php的“root”命名空间?和具有相同Class 'Namespace\Example' not found错误消息的那个:Calling a static method from a class in another namespace in PHP。
回答by kDjakman
Looks to me that the line is trying to throw a user defined Exception in the current namespace, not the built-in Exception class of PHP itself
在我看来,该行试图在当前命名空间中抛出用户定义的 Exception,而不是 PHP 本身的内置 Exception 类
回答by VSP
Extending @hakre answer you can simplify its usage with:
扩展@hakre 答案,您可以通过以下方式简化其用法:
use \Exception as Exception;
That way you can throw exceptions without remembering the backslash like:
这样您就可以在不记住反斜杠的情况下抛出异常,例如:
throw new Exception('This bundle needs the cURL PHP extension.');

