Catch exception when querying the database