add try catch in foreach