PHP-5.3:Class Exception Not Found

If in PHP 5.3 (likely just PHP 5) you define a namespace for your current class and try to throw a new Exception, it will fail with the following error

Fatal error: Class 'your\current\class\namespace\Exception' not found in YourClass.php on line eleventy-billion.'

The problem is that when PHP fails to find a class in the current namespace (other than root of course), it doesn’t automagically search the root namespace. The Exception object exists in the root namespace (unless you created your own) so PHP won’t find an it because it doesn’t exist in your class’s defined namespace.

The solution is to define the root namespace before your new Exception object.

throw new \Exception('This will do nicely');

Category:PHP