Bird
0
0

Identify the issue in this Ruby code snippet:

medium📝 Debug Q6 of 15
Ruby - Error Handling
Identify the issue in this Ruby code snippet:
begin
  puts 10 / 0
rescue
  puts 'General error caught'
rescue ZeroDivisionError
  puts 'Cannot divide by zero'
end
AMultiple rescue clauses are used incorrectly; only one rescue block is allowed per begin
BThe code will run without errors and print both messages
CThe ZeroDivisionError rescue will always execute before the general rescue
DThe rescue blocks should be replaced with else blocks
Step-by-Step Solution
Solution:
  1. Step 1: Understand rescue usage

    Ruby allows only one rescue block per begin statement, but it can handle multiple exceptions in that block.
  2. Step 2: Analyze the code

    This code has two separate rescue blocks, which is invalid syntax.
  3. Final Answer:

    Multiple rescue clauses are used incorrectly; only one rescue block is allowed per begin -> Option A
  4. Quick Check:

    Are multiple rescue blocks allowed? No. [OK]
Quick Trick: Only one rescue block per begin allowed [OK]
Common Mistakes:
  • Assuming multiple rescue blocks are valid
  • Thinking rescue order affects execution
  • Confusing rescue with else or ensure

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes