Add this to the top of your file.
use Exception;
Because if your file is in a namespace you will need to add it.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I'm following the course PHP Practitioner and I'm having some problems with displaying Exceptions. My code is this:
public function direct($uri, $requestType)
{
if (array_key_exists($uri, $this->routes[$requestType])) {
return $this->callAction(
...explode('@', $this->routes[$requestType][$uri])
);
}
throw new Exception('No route defined for this URI.');
}
But when I run it and try a route that I know won't work I get a Fatal Error instead. It says:
Fatal error: Uncaught Error: Class 'App\Core\Exception' not found in ...
Now I haven't declared Exception or anything, do I need to add anything like that to use it? Thanks
Please or to participate in this conversation.