基本上,我想做这样的事情(用Python或类似的命令式语言):
for i in xrange(1, 5):
try:
do_something_that_might_raise_exceptions(i)
except:
continue # continue the loop at i = i + 1
我如何在Ruby中执行此操作?我知道有redo
和retry
关键字,但它们似乎重新执行“try”块,而不是继续循环:
for i in 1..5
begin
do_something_that_might_raise_exceptions(i)
rescue
retry # do_something_* again, with same i
end
end
答案 0 :(得分:110)
在Ruby中,continue
拼写为next
。
答案 1 :(得分:49)
for i in 1..5
begin
do_something_that_might_raise_exceptions(i)
rescue
next # do_something_* again, with the next i
end
end
答案 2 :(得分:6)
打印例外:
rescue
puts $!, $@
next # do_something_* again, with the next i
end