Error handling inside while