How to use "retry" into two deep begin/rescue/end?

Hi, in the code below I want “retry/break/XXXXX” (undefined) in SECOND
BEGIN
to go back to FIRST BEGIN when “raise” instead of SECOND BEGIN but it
goes to
SECOND BEGIN.

I think it’s the expected behaviour but maybe there is a way to get what
I
want, is it possible?


class MyTcpServer < GServer

def serve(io)

loop do
begin # FIRST BEGIN
puts “welcome”


begin # SECOND BEGIN
raise
rescue
puts “ERROR”
retry/break/return/XXXXX # Restart from FIRST BEGIN (doesn’t
work)
end


rescue
$stderr.print "serve(io) ERROR: " + $!
end
end # loop do

end # def serve(io)

Thanks a lot.

On Apr 2, 6:27 pm, Iñaki Baz C. [email protected] wrote:

Hi, in the code below I want “retry/break/XXXXX” (undefined) in SECOND BEGIN
to go back to FIRST BEGIN when “raise” instead of SECOND BEGIN but it goes to
SECOND BEGIN.

I think it’s the expected behaviour but maybe there is a way to get what I
want, is it possible?

Can you combine the two?

                    ...
                    ...
            rescue NameError # or whatever class of error you want to catch
                    $stderr.print "serve(io) ERROR: " + $!
              rescue # most other stuff

From: Iñaki Baz C. [mailto:[email protected]]

loop do

begin # FIRST BEGIN

puts “welcome”

begin # SECOND BEGIN

raise

rescue

puts “ERROR”

retry/break/return/XXXXX

Restart from FIRST BEGIN (doesn’t work)

end

rescue

$stderr.print "serve(io) ERROR: " + $!

end

end # loop do

end # def serve(io)

your first begin is anchored on a loop, so you can use retry.

eg,

C:\family\ruby>cat test.rb
loop do
begin # FIRST BEGIN
puts “first welcome”

begin  # SECOND BEGIN
  raise "triggered ERROR in second begin"
rescue =>e
  puts "rescuing error: "+e.message
  puts "retrying to first welcome.."
  sleep 1 # put this to slow down
  next    # this goes back to next loop
end

rescue
puts “Error in first welcome”
end
puts “this is never executed in this example :)”
end

C:\family\ruby>ruby test.rb
first welcome
rescuing error: triggered ERROR in second begin
retrying to first welcome…
first welcome
rescuing error: triggered ERROR in second begin
retrying to first welcome…
first welcome
rescuing error: triggered ERROR in second begin
retrying to first welcome…
first welcome
rescuing error: triggered ERROR in second begin
retrying to first welcome…
test.rb:10:in `sleep’: Interrupt
from test.rb:10

kind regards -botp

your first begin is anchored on a loop, so you can use retry.

sorry typo, that should be “next” instead, as show in example :slight_smile:

El Jueves, 3 de Abril de 2008, yermej
escribió:> > rescue NameError # or whatever class of error you want to

catch $stderr.print "serve(io) ERROR: " + $!

              rescue # most other stuff
                    puts "ERROR"
                    retry/break/return/XXXXX  # Restart from FIRST >
            end end  # loop do

But what I want is going back to the previous/parent loop.

El Jueves, 3 de Abril de 2008, Peña, Botp escribió:

  sleep 1 # put this to slow down

rescuing error: triggered ERROR in second begin
retrying to first welcome…
first welcome

Yeah !! thanks a lot, I was looking for it but I’ve not found that
“next”
command anywhere. Thanks :wink:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs