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

**URL:** <https://www.ruby-forum.com/t/how-to-use-retry-into-two-deep-begin-rescue-end/133731>\
**Category:** Ruby\
**Created:** [April 3, 2008, 1:29am UTC](https://www.ruby-forum.com/t/how-to-use-retry-into-two-deep-begin-rescue-end/133731 "2008-04-03T01:29:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [April 3, 2008, 1:29am UTC](https://www.ruby-forum.com/t/how-to-use-retry-into-two-deep-begin-rescue-end/133731/1 "2008-04-03T01:29:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [April 3, 2008, 2:01am UTC](https://www.ruby-forum.com/t/how-to-use-retry-into-two-deep-begin-rescue-end/133731/2 "2008-04-03T02:01:37Z")

</div>

On Apr 2, 6:27 pm, Iñaki Baz C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

```

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [April 3, 2008, 5:14am UTC](https://www.ruby-forum.com/t/how-to-use-retry-into-two-deep-begin-rescue-end/133731/3 "2008-04-03T05:14:00Z")

</div>

From: IÃ±aki Baz C. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# 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

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [April 3, 2008, 5:20am UTC](https://www.ruby-forum.com/t/how-to-use-retry-into-two-deep-begin-rescue-end/133731/4 "2008-04-03T05:20:26Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [April 3, 2008, 9:35pm UTC](https://www.ruby-forum.com/t/how-to-use-retry-into-two-deep-begin-rescue-end/133731/5 "2008-04-03T21:35:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [April 3, 2008, 9:38pm UTC](https://www.ruby-forum.com/t/how-to-use-retry-into-two-deep-begin-rescue-end/133731/6 "2008-04-03T21:38:20Z")

</div>

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 😉
