Ruby:捕捉exception后继续循环
基本上,我想要做这样的事情(用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
在Ruby中, continue
是拼写。
for i in 1..5 begin do_something_that_might_raise_exceptions(i) rescue next # do_something_* again, with the next i end end
打印exception:
rescue puts $!, $@ next # do_something_* again, with the next i end