try-except inside while loop