didi_X8
didi_X8

Reputation: 5068

PHP "Exception not found"

I have a somehow funny issue. While trying to understand why a certain website returns http code 500 to browser, I found the message

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:

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?

Upvotes: 21

Views: 41475

Answers (3)

VSP
VSP

Reputation: 2393

Extending @hakre answer you can simplify its usage with:

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.');

Upvotes: 2

hakre
hakre

Reputation: 197564

The MZMailChimpBundle does not contain a class named Exception within the MZ\MailChimpBundle\Services namespace.

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.

The original has meant \Exception and not Exception here. It's a somewhat common mistake that can happen with namespaces. To fix the file, either alias/import \Exception as Exception:

namespace MZ\MailChimpBundle\Services;
use Exception;

and/or change the new line in 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 found error message: Calling a static method from a class in another namespace in PHP.

Upvotes: 44

kDjakman
kDjakman

Reputation: 116

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

Upvotes: 1

Related Questions