How to return a 404 when custom query matches nothing?

How to return a 404 when custom query matches nothing?

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)