Need suggestion how can I skip the rest par inside a loop when erro is raised

13 views
Skip to first unread message

Love U Ruby

unread,
Sep 12, 2013, 9:41:28 AM9/12/13
to rubyonra...@googlegroups.com
I need one suggestions from you :-

Suppose I do have a code as below:

until row == nil do
#code1
begin
#code2 <~~ which can raise an error
rescue
end
#here I want some guard which can check,if any error occurred or
not.If #error then skip below part and continue from the next row.
#code3
row+=1
end

--
Posted via http://www.ruby-forum.com/.

Joel Pearson

unread,
Sep 12, 2013, 10:56:19 AM9/12/13
to rubyonra...@googlegroups.com
Just use error handling properly.

begin
#standard code
rescue
#only runs if an error occurred
else
#only runs if an error did not occur
ensure
#always runs at the end regardless of error level

Scott Ribe

unread,
Sep 12, 2013, 11:21:58 AM9/12/13
to rubyonra...@googlegroups.com
On Sep 12, 2013, at 8:56 AM, Joel Pearson <li...@ruby-forum.com> wrote:

> Just use error handling properly.
>
> begin
> #standard code
> rescue
> #only runs if an error occurred
> else
> #only runs if an error did not occur
> ensure
> #always runs at the end regardless of error level
> end

It seems to me that OP may not even understand:

begin
#standard code
#only runs if no error earlier in this block
rescue
#only runs if an error occurred
end
#always runs as long as all exceptions are caught and none are re-thrown

--
Scott Ribe
scott...@elevated-dev.com
http://www.elevated-dev.com/
(303) 722-0567 voice




Love U Ruby

unread,
Sep 12, 2013, 11:47:52 AM9/12/13
to rubyonra...@googlegroups.com
Joel Pearson wrote in post #1121289:
> Just use error handling properly.
>
> begin
> #standard code
> rescue

I am looking for the below operations,but only when error will be
occured :

a = (1..10)
i=0
until a.size>10
next i+=1 if i<5 # this should be done on Error
p a[i]
i+=1
end

If still I am not clear,please let me know the confusions.

until val == nil
#line1
#line2 #<~~ error can be generated here.If error happened I want to
start
from #line1 again.skipping the below code.
#line3
...
end

Thanks

Joel Pearson

unread,
Sep 12, 2013, 12:22:52 PM9/12/13
to rubyonra...@googlegroups.com
Maybe a real example would help, rather than infinite-loop pseudo-code.

val = -3
until val > 2
begin
val +=1
1/val
rescue
puts 'Error on ' + val.to_s
else
puts 'No Error on ' + val.to_s
end
puts 'Finished with ' + val.to_s
Reply all
Reply to author
Forward
0 new messages