Hide block on 404 (not found) page SITUATION:

A View page displays all &quot;items&quot; that have a certain &quot;code&quot;.

The code is passed via a contextual filter, e.g. /items/17, where 17 is the code.

A user might try to guess a code by typing it in, e.g. /items/345.

When the View is empty (i.e. the code is bogus), a not-found exception is raised as follows:
function mymodule_views_pre_render(\Drupal\views\ViewExecutable $view) {
  if ($view-&gt;current_display == 'page_machine_name' &amp;&amp; empty($view-&gt;result)) {
    throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException();
  }
}


There is a menu included as a block that's displayed at the top of the View page.

The menu block configuration restricts the block to pages with paths /items/*.


PROBLEM:
The menu also appears on the 404 page, since that page retains the bogus path /items/345. How can I exclude the menu block from the 404 page?
NOTES:

I want to display the default 404 page rather than the view with a &quot;not found&quot; message as its body.
I don't want to display the view as a block instead of as a page unless it's the only way. I already know how to solve the problem if I have to do that.
If there were a way to say, &quot;Include this block only on View pages&quot; (like you can for content types), that would be ideal.
There are actually three blocks that need to be excluded from the 404 page; I mention only one to make it simpler. The other blocks are a text block and a banner block.
I see that a solution to this problem has been proposed for Drupal 9, but not available yet.
Yes, the solution is not available yet in core, see https://www.drupal.org/node/2245767.
But you can use this contrib module https://www.drupal.org/project/block_in_page_not_found and negate the condition to not show the block on a 404 page: