Bird
0
0

Find the error in this Ruby code snippet:

medium📝 Debug Q14 of 15
Ruby - Error Handling

Find the error in this Ruby code snippet:

begin
  raise 'Oops!'
rescue IOError
  puts 'IO error caught'
end
AThe raise statement must specify an exception class
BThe rescue clause will not catch the raised error
CThe rescue clause syntax is incorrect
DNo error; code runs fine
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the raised error

    Raising with a string 'Oops!' creates a RuntimeError by default.
  2. Step 2: Check rescue clause type

    The rescue only catches IOError, but the raised error is RuntimeError, so it is not caught.
  3. Final Answer:

    The rescue clause will not catch the raised error -> Option B
  4. Quick Check:

    Rescue type mismatch causes uncaught error [OK]
Quick Trick: Raised error type must match rescue type [OK]
Common Mistakes:
  • Thinking rescue catches all errors by default
  • Believing string raise is invalid syntax
  • Assuming rescue syntax is wrong

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes