Bird
0
0

What will happen when this Ruby code runs?

medium📝 Predict Output Q5 of 15
Ruby - Error Handling
What will happen when this Ruby code runs?
class MyError < StandardError; end

begin
  raise MyError
rescue StandardError
  puts "Rescued!"
end
ARaises an unhandled exception error
BPrints nothing
CPrints "Rescued!"
DSyntax error
Step-by-Step Solution
Solution:
  1. Step 1: Analyze raise and rescue

    MyError inherits from StandardError, so rescue StandardError catches it.
  2. Step 2: Check output

    The rescue block runs and prints "Rescued!".
  3. Final Answer:

    Prints "Rescued!" -> Option C
  4. Quick Check:

    Subclass exceptions caught by parent rescue [OK]
Quick Trick: Rescue StandardError catches subclasses too [OK]
Common Mistakes:
  • Thinking rescue won't catch subclass exceptions
  • Expecting unhandled exception error
  • Confusing syntax error with runtime behavior

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes