[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

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

Iñaki Baz Castillo

4/2/2008 11:28:00 PM

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

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


=2D-----------------------------------------------------------
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)
=2D-----------------------------------------------------------


Thanks a lot.


=2D-=20
I=C3=B1aki Baz Castillo

4 Answers

yermej

4/2/2008 11:58:00 PM

0

On Apr 2, 6:27 pm, Iñaki Baz Castillo <i...@aliax.net> 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?

> 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
> puts "ERROR"
> retry/break/return/XXXXX # Restart from FIRST > end
> end # loop do
>
> end # def serve(io)
> ------------------------------------------------------------
>
> Thanks a lot.
>
> --
> Iñaki Baz Castillo

Peña, Botp

4/3/2008 3:20:00 AM

0

IyB5b3VyIGZpcnN0IGJlZ2luIGlzIGFuY2hvcmVkIG9uIGEgbG9vcCwgc28geW91IGNhbiB1c2Ug
cmV0cnkuDQoNCnNvcnJ5IHR5cG8sIHRoYXQgc2hvdWxkIGJlICJuZXh0IiBpbnN0ZWFkLCBhcyBz
aG93IGluIGV4YW1wbGUgOikNCg==

Iñaki Baz Castillo

4/3/2008 7:35:00 PM

0

El Jueves, 3 de Abril de 2008, yermej escribi=F3:
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 rescue NameError # or whatever class of=
error you want to
> > catch $stderr.print "serve(io) ERROR: " + $!
>
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 rescue # most other stuff
>
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 puts "ERROR"
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 retry/break/return/XXXX=
X =A0# Restart from FIRST >
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 end end =A0# loop do

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

=2D-=20
I=F1aki Baz Castillo

Iñaki Baz Castillo

4/3/2008 7:36:00 PM

0

El Jueves, 3 de Abril de 2008, Pe=C3=B1a, Botp escribi=C3=B3:

> C:\family\ruby>cat test.rb
> loop do
> begin # FIRST BEGIN
> puts "first welcome"
>
> begin # SECOND BEGIN
> raise "triggered ERROR in second begin"
> rescue =3D>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


Yeah !! thanks a lot, I was looking for it but I've not found that "next"=20
command anywhere. Thanks ;)



=2D-=20
I=C3=B1aki Baz Castillo