Bird
0
0

Identify the error in this Ruby code:

medium📝 Debug Q6 of 15
Ruby - Error Handling
Identify the error in this Ruby code:
def example
  raise "Error occurred"
rescue
  puts "Rescued"
end

example
ANo error, code runs and prints "Rescued"
BSyntax error: rescue must be inside begin-end block
CRuntime error: raise cannot be used without rescue
DNo output because rescue is misplaced
Step-by-Step Solution
Solution:
  1. Step 1: Understand implicit begin in method

    In Ruby, method bodies have an implicit begin block, so rescue can be used directly.
  2. Step 2: Check code behavior

    The raise triggers an error, which is rescued, so "Rescued" is printed.
  3. Final Answer:

    No error, code runs and prints "Rescued" -> Option A
  4. Quick Check:

    Rescue works inside method without explicit begin = D [OK]
Quick Trick: Methods have implicit begin for rescue [OK]
Common Mistakes:
  • Thinking rescue needs explicit begin
  • Assuming raise without rescue crashes
  • Expecting no output

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes