Bird
0
0

What is the issue with this Ruby code snippet using retry?

medium📝 Debug Q6 of 15
Ruby - Error Handling
What is the issue with this Ruby code snippet using retry?
begin
  puts 'Processing'
  raise 'Error'
rescue
  retry
  puts 'Recovered'
end
AThe exception is not rescued properly.
BThe <code>retry</code> keyword is used outside the rescue block.
CThe code will cause a syntax error due to misplaced <code>retry</code>.
DThe <code>puts 'Recovered'</code> line is unreachable because <code>retry</code> restarts the block immediately.
Step-by-Step Solution
Solution:
  1. Step 1: Understand retry flow

    When retry is executed, control jumps back to the begin block immediately.
  2. Step 2: Analyze unreachable code

    Any code after retry inside rescue will never run.
  3. Step 3: Identify the problem

    Here, puts 'Recovered' is unreachable and thus ineffective.
  4. Final Answer:

    The puts 'Recovered' line is unreachable because retry restarts the block immediately. -> Option D
  5. Quick Check:

    Code after retry is never executed [OK]
Quick Trick: Code after retry in rescue is unreachable [OK]
Common Mistakes:
  • Expecting code after retry to run
  • Misplacing retry outside rescue
  • Confusing retry with next or break

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes