error catching in a while loop