There are situations where you need to trigger 404 error when building custom plugin. One example could be when you are querying data from database and there is no data for our query. This is how you do it:
use yii\web\NotFoundHttpException; ... throw new NotFoundHttpException('Page not found.');