Python中是否有任何方法可以在迭代器/生成器抛出异常后继续迭代?就像下面的代码一样,有没有办法跳过ZeroDivisionError并继续循环而gener()没有modyfying run()函数?
def gener():
a = [1,2,3,4,0, 5, 6,7, 8, 0, 9]
for i in a:
yield 2/i
def run():
for i in gener():
print i
#---- run script ----#
try:
run()
except ZeroDivisionError:
print 'what magick should i put here?'
Run Code Online (Sandbox Code Playgroud)
该逻辑位置try/except将是发生违规计算的地方:
def gener():
a = [1,2,3,4,0, 5, 6,7, 8, 0, 9]
for i in a:
try:
yield 2/i
except ZeroDivisionError:
pass
Run Code Online (Sandbox Code Playgroud)