I am trying to iterate through a list, and I need to perform specific operation when and only when the iteration reached the end of the list, see example below:
data = [1, 2, 3]
data_iter = data.__iter__()
try:
while True:
item = data_iter.next()
try:
do_stuff(item)
break # we just need to do stuff with the first successful item
except:
handle_errors(item) # in case of no success, handle and skip to next item
except StopIteration:
raise Exception("All items weren't successful")
I believe this code isn't too Pythonic, so I am looking for a better way. I think the ideal code should look like this hypothetical piece below:
data = [1, 2, 3]
for item in data:
try:
do_stuff(item)
break # we just need to do stuff with the first successful item
except:
handle_errors(item) # in case of no success, handle and skip to next item
finally:
raise Exception("All items weren't successful")
Any thoughts are welcome.