Bird
0
0

Identify the error in this Ruby code using retry:

medium📝 Debug Q14 of 15
Ruby - Error Handling
Identify the error in this Ruby code using retry:
begin
  puts 'Start'
  raise 'Oops'
rescue
  puts 'Error caught'
  retry
  puts 'After retry'
end
AThe puts 'After retry' statement is unreachable after retry.
Bretry cannot be used inside rescue block.
CMissing end for begin block.
DThe raise statement should be inside rescue.
Step-by-Step Solution
Solution:
  1. Step 1: Analyze retry placement

    The retry statement causes the code to jump back to begin, so any code after retry in rescue is never run.
  2. Step 2: Identify unreachable code

    The line puts 'After retry' is after retry and will never execute, making it unreachable.
  3. Final Answer:

    The puts 'After retry' statement is unreachable after retry. -> Option A
  4. Quick Check:

    Code after retry is unreachable = A [OK]
Quick Trick: Code after retry in rescue is never executed [OK]
Common Mistakes:
  • Thinking retry can't be inside rescue
  • Ignoring unreachable code warnings
  • Misplacing raise statement

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes