begin...rescue...retry next?

J

Joe Ruby

Is there some way to do this in Ruby?

begin
# do stuff, uh-oh an exception gets raised
rescue
retry next
end

There's 'retry' which returns to the same statement that threw the
exception, but I'd like execution to resume at the next statement
following it.

Thanks,
Joe
 
M

Max Muermann

There's 'retry' which returns to the same statement that threw the
exception, but I'd like execution to resume at the next statement
following it.

not_tried_before = true
begin
do_dangerous_stuff if not_tried_before
rescue
not_tried_before = false
retry
end

Max
 
L

Logan Capaldo

Is there some way to do this in Ruby?

begin
# do stuff, uh-oh an exception gets raised
rescue
retry next
end

There's 'retry' which returns to the same statement that threw the
exception, but I'd like execution to resume at the next statement
following it.

Thanks,
Joe

If you know only expression X will raise the exception you can use
the modifier form

some_code
some_exception_raising_call rescue nil # This is evil
continue_blissfully_unaware_of_whether_an_exception_was_raised

Also retry does not return to the statement that raised the
exception, it jumps to the begin

begin
# retry will put you here
some_code
some_more_code
raise_an_exception
even_more_code
rescue
retry
end
 
L

Logan Capaldo

Why is that evil?

Joe

Cause you can't specify which exceptions to catch with that form.
Someday something terrible could happen inside
some_exception_raising_call that you _do_ want to know about but
it'll just get thrown away by the rescue nil.
 
J

Justin Collins

Joe said:
Why is that evil?

Joe

Because if something goes wrong in that call, you will get no
information about it. Won't even know it happened.

-Justin
 
J

Joe Ruby

Justin said:
Because if something goes wrong in that call, you will get no
information about it. Won't even know it happened.

-Justin

Yeah, but rescue nil is just usually to deal with null values from the
database, for example, which isn't a big deal.

Joe
 
J

Justin Collins

Joe said:
Yeah, but rescue nil is just usually to deal with null values from the
database, for example, which isn't a big deal.

Jo

Using rescue this way will rescue all exceptions, but simply return nil.
Examples:

irb(main):001:0> puts a rescue nil
=> nil
irb(main):002:0> puts "asiodhasd".asdiajsd rescue nil
=> nil
irb(main):003:0> asdi.asdiojas.asidjas.asdihja.asdijasd rescue nil
=> nil
irb(main):004:0>

It's better, as I think Logan was implying, to rescue a specific
exception if you are expecting it. That way, you will see if other
exceptions occur.

Sorry if I misunderstood your reply.

-Justin
 
J

Joe Ruby

Aaahhh...all this time I thought I was rescuing nil rather than rescuing
everything and returning nil!

Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top