Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby Ruby Foundations Loops Control Flow

Eivind Jonassen
Eivind Jonassen
5,994 Points

Getting an error when practicing "retry" command in for-loops.

Hi! Getting the following errors when trying to run my code: "Invalid retry" "Compile error (SyntaxError) Here is the code:

animals = %w(dog cat horse goat snake frog)
count = 0

for animal in animals
    puts "The current animal is #{animal}"
    break if count == 10
    count += 1
    retry if animal == 'horse'
end

4 Answers

Chris Shaw
Chris Shaw
26,650 Points

Hi Eivind,

I literally just finished this course tonight but I did a little research and was able to gather that triggering an raise exception on if animal == 'horse' within a begin...end block works for Ruby 1.9+.

animals = %w(dog cat horse goat snake frog)
count = 0

animals.each do |animal|
    begin
        puts "The current animal is #{animal}"
        break if count == 10
        count += 1
        raise if animal == 'horse'
    rescue Exception
        retry
    end
end

This is not an ideal solution but for the purpose of this example it works, I tested this in workspaces and it gave me the desired result, happy coding =)

/cc Jason Seifer

Eivind Jonassen
Eivind Jonassen
5,994 Points

This is no longer valid syntax it turns out.. An earlier post about the same issue has Jason Seifer commenting this:

Hey all, we're going to update these videos. They were filmed on an older version of Ruby where this syntax works. We'll be updating them shortly. In the mean time, you can skip the retry part. Sorry for the trouble!

This was 10 months ago. Nor sure what shortly means in Treehouse's vocabulary.. :p

Ricardo Garcia
Ricardo Garcia
5,939 Points

im still waiting for the updated videos :S

Matthew Pierce
Matthew Pierce
15,779 Points

16 months and still waiting!

Antoine Boillot
Antoine Boillot
10,466 Points

Hi Chris,

I tried your code but cannot get the same result as Jason in the video.

Here is my code (looks like it is the same as you did) :

animals = %w(dog cat horse goat snake frog)
count = 0

animals.each do |animal|
    begin
        puts "The current animal is #{animal}"
        break if count == 10
        count += 1
        raise if animal == 'horse'
    rescue Exception
        retry
    end
end

and here is the input I get in the console :

The current animal is dog
The current animal is cat
The current animal is horse
The current animal is horse
The current animal is horse
The current animal is horse
The current animal is horse
The current animal is horse
The current animal is horse
The current animal is horse
The current animal is horse

Instead of :

The current animal is dog
The current animal is cat
The current animal is horse
The current animal is dog
The current animal is cat
The current animal is horse
The current animal is dog
The current animal is cat
The current animal is horse
The current animal is dog
The current animal is cat

I'm using Ruby 2.1.3

Do you know why it is not working ?

Thanks a lot

Antoine Boillot
Antoine Boillot
10,466 Points

I just tried something else :

animals = %w(dog cat horse goat snake frog)
count = 0

begin
    animals.each do |animal|
        puts "The current animal is #{animal}"
        break if count == 10
        count += 1
        raise if animal == 'horse'
    end
rescue
    retry
end

And it works fine now.

Christine Merey
Christine Merey
3,309 Points

Chris Upjohn and Antoine Boillot, I read on SO that

rescue Exception

is a (very) bad idea. It's worth a look. It includes the intriguing quote:

"Don’t rescue Exception. EVER. or I will stab you."

http://stackoverflow.com/questions/10048173/why-is-it-bad-style-to-rescue-exception-e-in-ruby

Much better to use

rescue

Since the default is rescue StandardError

To quote Daniel Fone: "So…

… if you encounter rescue Exception => e in an existing codebase, you can almost certainly replace it with rescue => e.

… if you find yourself about to type rescue Exception => e, slap yourself in the face, figure out exactly what exceptions you’re dealing with and rescue those instead."

http://daniel.fone.net.nz/blog/2013/05/28/why-you-should-never-rescue-exception-in-ruby/