-
Notifications
You must be signed in to change notification settings - Fork 0
Exceptions
Muhammet Şafak edited this page Jun 9, 2026
·
1 revision
All router exceptions live under the InitPHP\Router\Exception namespace.
| Exception | Extends | Thrown when |
|---|---|---|
RouterException |
\RuntimeException |
A controller/middleware can't be resolved, a route name is duplicated, a filter is invalid, a parameter can't be resolved, the route cache can't be read, etc. |
PageNotFoundException |
RouterException |
No route matched the request and no 404 handler is defined. |
InvalidArgumentException |
\InvalidArgumentException |
An argument has an invalid type or value (an unsupported HTTP method, a bad handler type, an invalid IP address, a missing link source). |
\RuntimeException
└─ InitPHP\Router\Exception\RouterException
└─ InitPHP\Router\Exception\PageNotFoundException
\InvalidArgumentException
└─ InitPHP\Router\Exception\InvalidArgumentException
Because PageNotFoundException extends RouterException, catching
RouterException also catches 404s — order your catch blocks accordingly.
use InitPHP\Router\Exception\PageNotFoundException;
use InitPHP\Router\Exception\RouterException;
try {
$response = $router->dispatch();
} catch (PageNotFoundException $e) {
// No route matched and no 404 handler was set.
$response = $response->withStatus(404);
} catch (RouterException $e) {
// Any other router runtime error.
$response = $response->withStatus(500);
}The cleanest way to render a 404 is to register a handler with
setNotFoundHandler() so no exception is thrown at all — see
Error Handling.
See also: API Reference · FAQ.
initphp/router · MIT License · part of the InitPHP family
Source · Issues · Discussions · Packagist · Contributing · Security Policy
Getting Started
Defining Routes
Handling Requests
Reference
Migration