404 is generated by the router when it detects it has nowhere to route, and it will do that by throwing an HttpNotFoundException.
If you don't have any code in place to capture that exception, it will bubble up to your index.php, where it is caught, and a new request is constructed for the defined 404 route. Uri functions work in the context of a request.