Python 有没有办法在迭代器/生成器抛出异常后继续迭代?就像下面的代码一样,有没有办法跳过 ZeroDivisionError 并继续循环gener()
不修改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?'
的逻辑位置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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)