我需要在索引列表时不断捕获异常抛出 IndexError 异常,例如:
l = []
while((l[3]) throws IndexError):
//Add more data to the list
l += [3]
如何在不嵌套嵌套 try/catch block 的情况下继续检查方法调用是否引发异常?
最佳答案
这取决于您想用什么来扩展您的列表。假设“无”,你可以这样做:
l = []
while True:
try:
l[3] = 'item'
break
except IndexError:
l.extend([None])
print l # [None, None, None, 'item']
关于python - python中不断捕获异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22451472/