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)
------------------------------------------------------------
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?
class MyTcpServer < GServer
def serve(io)
loop do
begin # FIRST BEGIN
puts "welcome"
...
...
raise
...
...
rescue NameError # or whatever class of error you want to catch
$stderr.print "serve(io) ERROR: " + $!
rescue # most other stuff
···
On Apr 2, 6:27 pm, Iñaki Baz Castillo <i...@aliax.net> wrote:
puts "ERROR"
retry/break/return/XXXXX # Restart from FIRST > end
end # loop do
end # 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
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
Yeah !! thanks a lot, I was looking for it but I've not found that "next"
command anywhere. Thanks
···
El Jueves, 3 de Abril de 2008, Peña, Botp escribió:
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