Ruby: Continue the loop after detecting an exception

Basically, I want to do something like this (in Python or similar imperative languages):

for i in xrange(1, 5): try: do_something_that_might_raise_exceptions(i) except: continue # continue the loop at i = i + 1 

How to do it in Ruby? I know there are redo and retry , but they seem to re-execute the try block, rather than continuing the loop:

 for i in 1..5 begin do_something_that_might_raise_exceptions(i) rescue retry # do_something_* again, with same i end end 
+47
ruby loops exception-handling
Apr 12 2018-10-12T00:
source share
3 answers

Ruby continue says next .

+93
Apr 12 '10 at 19:39
source share
 for i in 1..5 begin do_something_that_might_raise_exceptions(i) rescue next # do_something_* again, with the next i end end 
+44
Nov 11 2018-10-11
source share

for printing exceptions:

 rescue puts $!, $@ next # do_something_* again, with the next i end 
+6
Jan 23 '13 at 3:51
source share



All Articles