The Retry statement
Retry statements are used to resume an iterator based on any method call from the beginning of a specific condition. To put it simply, the retry statement starts a new process with control. Retry statements are typically not used frequently.
Some functions merit another go. There are many situations where one could desire for the beginning block to make amends. For instance, as a busy server takes longer to respond, HTTP requests and database calls occasionally fail or time out on the first try. Before giving up in such circumstances, we might want to rerun the block a few more times.
Only Ruby version 1.8 will support it.
Since the retry statement is regarded as a deprecated language feature, it has been eliminated from Ruby versions 1.9 and beyond. Most online IDEs utilize version 1.8 or higher, it may never run on them.
Examples for Retry Statement
Example 1
The following Ruby program demonstrates the use of the retry statement.
#!/usr/bin/ruby
begin
for x in 1..10
raise if x > 3
puts "Value of the local variable is #{x}."
end
rescue
retry
end

You can also try this code with Online Ruby Compiler
Run Code
Output:

The output goes on in an infinite loop.
Example 2
The following example again illustrates the use of the retry statement.
#!/usr/bin/ruby
begin
attemptAgain ||= 0
puts "Attempt again to execute the code block #{ attemptAgain }"
raise "We are raising this exception each time to check the count of attemptAgain"
rescue
puts "Ruby 'retry' statement is called"
retry if (attemptAgain += 1) < 5
end

You can also try this code with Online Ruby Compiler
Run Code
Output:

Example 3
#!/usr/bin/ruby
# Use of the 'do' loop
4.times do |x|
begin
puts "Iteration No. #{x}"
raise if x > 2
rescue
# Use of the 'retry' statement
puts "Here the retry statement is called as it will be falling under rescue block #{x}"
# retry
end
end

You can also try this code with Online Ruby Compiler
Run Code
Output:

If we undo the comment from line no. 10 and use the ‘retry’ statement, then the output will be like this:
Iteration No. 0
Iteration No. 1
Iteration No. 2
Iteration No. 3
Here the retry statement is called as it will be falling under rescue block 3
Iteration No. 3
Here the retry statement is called as it will be falling under rescue block 3
Iteration No. 3
Here the retry statement is called as it will be falling under rescue block 3
Iteration No. 3
.
.
.
The output goes on in an infinite loop.
Note: The statement retry: altering the control flow in Ruby only works within the begin/rescue block.
Frequently Asked Questions
What is retry: altering the control flow in Ruby phenomenon?
A control flow construct is a language feature that alters the natural flow of the source code by conditionally or unconditionally branching to a different statement or location. Programming and web development both depend on control flow.
What are loops in Ruby?
Programming languages provide looping feature that makes it possible to continually execute a set of instructions or functions depending on whether any of the conditions are true or false. Ruby has various loop types for handling condition-based scenarios in programs, making the programmer's job easier. The Ruby loops are while loop, for loop, do..while loop, and until loop.
What is retry in Ruby?
retry is mainly utilized in Ruby's exception handling environment. Control shifts to the rescue block, where the exception is handled when your program runs into an error inside a begin block. The execution of the code under the rescue block often comes next.
What is the difference between redo and retry statements?
To repeat a loop section, we can either redo or retry. Redo merely repeats the current iteration, whereas retry repeats the entire loop. However, they differ in how much they re-execute.
What are the different types of retry statements in Ruby?
The first type is a plain retry statement, which repeatedly runs a code block until it succeeds. A retry-with-exception statement, which will execute the code block once more but throws an exception if it fails, is the second type.
Conclusion
In this article, we have extensively discussed the retry: altering the control flow in Ruby phenomenon. We began with a brief introduction to Ruby, followed by a list of all the statements that alter the control flow, and looked at retry: altering the control flow in Ruby in detail.
After reading about the retry: altering the control flow in Ruby, are you not feeling excited to read/explore more articles on the Ruby programming language? Don't worry; Coding Ninjas has you covered. To learn, see Ruby, Ruby coding Ninjas, Documentation, Official Ruby FAQ, and Ruby Koans.
Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and Algorithms, Competitive Programming, JavaScript, System Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc; you must look at the problems, interview experiences, and interview bundle for placement preparations.
Nevertheless, you may consider our courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!
Happy Learning!