diff --git a/core/lib/Drupal/Core/Routing/AccessAwareRouter.php b/core/lib/Drupal/Core/Routing/AccessAwareRouter.php
index ba5523d7f0b357dea1acdd8a382d5701d1de8621..07b143431e2fa10f85c1dfc92232dd3e1d1ca484 100644
--- a/core/lib/Drupal/Core/Routing/AccessAwareRouter.php
+++ b/core/lib/Drupal/Core/Routing/AccessAwareRouter.php
@@ -7,8 +7,10 @@
 use Drupal\Core\Cache\CacheableDependencyInterface;
 use Drupal\Core\Http\Exception\CacheableAccessDeniedHttpException;
 use Drupal\Core\Session\AccountInterface;
+use Symfony\Component\HttpFoundation\Exception\BadRequestException;
 use Symfony\Component\HttpFoundation\Request;
 use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
+use Symfony\Component\Routing\Exception\ResourceNotFoundException;
 use Symfony\Component\Routing\RouteCollection;
 use Symfony\Component\Routing\RequestContext as SymfonyRequestContext;
 use Symfony\Component\Routing\RouterInterface;
@@ -138,7 +140,13 @@ public function generate($name, $parameters = [], $referenceType = self::ABSOLUT
    *   Thrown when access checking failed.
    */
   public function match($pathinfo): array {
-    return $this->matchRequest(Request::create($pathinfo));
+    try {
+      $request = Request::create($pathinfo);
+    }
+    catch (BadRequestException $e) {
+      throw new ResourceNotFoundException($e->getMessage(), $e->getCode(), $e);
+    }
+    return $this->matchRequest($request);
   }
 
 }
diff --git a/core/lib/Drupal/Core/Routing/Router.php b/core/lib/Drupal/Core/Routing/Router.php
index 369424d90815df958b6abd241070d435e19dd3cf..69d308846296682b8a4d86b88777461bb25cb6f2 100644
--- a/core/lib/Drupal/Core/Routing/Router.php
+++ b/core/lib/Drupal/Core/Routing/Router.php
@@ -3,6 +3,7 @@
 namespace Drupal\Core\Routing;
 
 use Drupal\Core\Path\CurrentPathStack;
+use Symfony\Component\HttpFoundation\Exception\BadRequestException;
 use Symfony\Component\HttpFoundation\Request;
 use Symfony\Component\Routing\Exception\MethodNotAllowedException;
 use Symfony\Component\Routing\Exception\ResourceNotFoundException;
@@ -98,7 +99,12 @@ public function addRouteEnhancer(EnhancerInterface $route_enhancer) {
    * {@inheritdoc}
    */
   public function match($pathinfo): array {
-    $request = Request::create($pathinfo);
+    try {
+      $request = Request::create($pathinfo);
+    }
+    catch (BadRequestException $e) {
+      throw new ResourceNotFoundException($e->getMessage(), $e->getCode(), $e);
+    }
 
     return $this->matchRequest($request);
   }
diff --git a/core/tests/Drupal/Tests/Core/Routing/RouterTest.php b/core/tests/Drupal/Tests/Core/Routing/RouterTest.php
index 254cf4669313d74412588b404fbd325a02b10f6e..7f354473f0a054339def0f0efba5b05a8a8b9e1c 100644
--- a/core/tests/Drupal/Tests/Core/Routing/RouterTest.php
+++ b/core/tests/Drupal/Tests/Core/Routing/RouterTest.php
@@ -12,6 +12,7 @@
 use Drupal\Core\Routing\UrlGeneratorInterface;
 use Drupal\Tests\UnitTestCase;
 use Prophecy\Argument;
+use Symfony\Component\Routing\Exception\ResourceNotFoundException;
 use Symfony\Component\Routing\Route;
 use Symfony\Component\Routing\RouteCollection;
 
@@ -59,6 +60,9 @@ public function testMatchesWithDifferentFitOrder(): void {
     $result = $router->match('/user/login');
 
     $this->assertEquals('user_login', $result['_route']);
+
+    $this->expectException(ResourceNotFoundException::class);
+    $router->match('/user/login ');
   }
 
 }