【发布时间】:2020-08-03 04:49:46
【问题描述】:
我希望循环继续进行,即使在第一次迭代时生成异常。如何做到这一点?
mydict = {}
wl = ["test", "test1", "test2"]
try:
for i in wl:
a = mydict['sdf']
print(i)
except:
# I want the loop to continue and print all elements of list, instead of exiting it after exception
# exception will occur because mydict doesn't have 'sdf' key
pass
【问题讨论】:
-
在循环体内移动
try/except