Bird
0
0

What will be the output of this Ruby code?

medium📝 Predict Output Q4 of 15
Ruby - Error Handling
What will be the output of this Ruby code?
class MyError < StandardError; end

begin
  raise MyError, "Oops!"
rescue MyError => e
  puts e.message
end
AStandardError
BMyError
CNo output
DOops!
Step-by-Step Solution
Solution:
  1. Step 1: Understand raise and rescue behavior

    The code raises MyError with message "Oops!" and rescues it, storing in e.
  2. Step 2: Check what is printed

    e.message returns the error message "Oops!", so it prints that.
  3. Final Answer:

    Oops! -> Option D
  4. Quick Check:

    Raised error message prints = "Oops!" [OK]
Quick Trick: e.message prints the error message string [OK]
Common Mistakes:
  • Printing the class name instead of message
  • Expecting no output because of rescue
  • Confusing error class with message

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes