added try and except for for loop