Wrapping try catch in while loop