Bird
0
0

What will this Ruby code output?

medium📝 Predict Output Q5 of 15
Ruby - Error Handling
What will this Ruby code output?
begin
  raise SystemExit
rescue Exception => e
  puts 'Rescued'
end
ANo output, program exits
BRuntimeError
CRescued
DSyntaxError
Step-by-Step Solution
Solution:
  1. Step 1: Understand raised exception

    The code raises SystemExit, which inherits from Exception.
  2. Step 2: Rescue block catches Exception

    The rescue catches all exceptions inheriting from Exception, including SystemExit, so it prints 'Rescued'.
  3. Final Answer:

    Rescued -> Option C
  4. Quick Check:

    Rescue Exception catches SystemExit [OK]
Quick Trick: Rescue Exception catches all exceptions including SystemExit [OK]
Common Mistakes:
  • Assuming SystemExit cannot be rescued
  • Expecting program to exit immediately
  • Confusing rescue Exception with rescue StandardError

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes