Bird
0
0

Identify the problem in this Ruby code snippet:

medium📝 Debug Q7 of 15
Ruby - Error Handling
Identify the problem in this Ruby code snippet:
begin
  # code
rescue ZeroDivisionError
  puts 'Zero division error'
rescue
  puts 'Generic error'
rescue IOError
  puts 'IO error'
end
AThe IOError rescue clause after generic rescue is unreachable.
BThe ZeroDivisionError rescue clause must be last.
CGeneric rescue clause cannot be used with other rescues.
DThe code is correct as is.
Step-by-Step Solution
Solution:
  1. Step 1: Analyze rescue clause order

    The generic rescue clause without exception type catches all exceptions.
  2. Step 2: Check if any rescue clauses after generic rescue are reachable

    Rescue clauses after generic rescue are unreachable because generic rescue catches all exceptions.
  3. Final Answer:

    The IOError rescue clause after generic rescue is unreachable. -> Option A
  4. Quick Check:

    Generic rescue must be last [OK]
Quick Trick: Generic rescue clause blocks later rescues [OK]
Common Mistakes:
  • Placing specific rescue after generic rescue
  • Assuming all rescue clauses run
  • Ignoring rescue clause order

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes