Bird
0
0

What will be the output of this Ruby code?

medium📝 Predict Output Q13 of 15
Ruby - Error Handling

What will be the output of this Ruby code?

begin
  raise IOError, 'File error'
rescue StandardError => e
  puts e.class
end
AStandardError
BException
CRuntimeError
DIOError
Step-by-Step Solution
Solution:
  1. Step 1: Identify the raised exception

    The code raises an IOError with message 'File error'.
  2. Step 2: Check rescue clause

    The rescue catches StandardError and its subclasses. IOError is a subclass of StandardError, so it is caught.
  3. Step 3: Output the exception class

    The code prints e.class, which is IOError.
  4. Final Answer:

    IOError -> Option D
  5. Quick Check:

    Raised error class = IOError [OK]
Quick Trick: Raised error class prints even if rescued [OK]
Common Mistakes:
  • Assuming rescue prints StandardError
  • Confusing Exception with StandardError
  • Thinking RuntimeError is printed

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes