re-implement while loop for exception handling