There are two ways to display 404 page in Magento.
1. We can call default CMS page of 404 which is already created in back-end.
2. We can call the noroute.phtml file for 404

Sometimes, the module is disabled or  it does not display any content in the site as per below screen shot.

enable configuration

and displays the blank page, which doesn’t look good and is not a good impression.

blank page

So a better solution is to display a 404 page.

Lets start How we can add the 404 code.

We can call default CMS page of 404 which is already created in back-end.

1. Add this code in the controller file, if module is disabled or the condition is not fulfilled.

We can call the noroute.phtml file to display a 404 page.

1. Add this code in the controller file, if module is disabled or the condition is not fulfilled.

2. Create the defaultNoRoute.php file in the path app/code/Webmull/{Module Name}/Controller/Index;

3. Create the {Module Name}_index_defaultnoroute.xml in app/code/{package Name}/{Module Name}/view/frontend/layout

Instead of this “Magento_Cms::default/no-route.phtml” default file, We can use this phtml file to display a 404 page.

And now refresh the page of your module to see the effect.