Bird
0
0

Given this code:

hard📝 Application Q9 of 15
Ruby - Error Handling
Given this code:
begin
  # code
rescue IOError
  puts 'IO error'
rescue
  puts 'Generic error'
end

What happens if an exception of type EOFError is raised, knowing EOFError inherits from IOError?
AThe exception is not caught and program crashes.
BThe generic rescue clause handles it.
CThe <code>IOError</code> rescue clause handles it.
DBoth rescue clauses run.
Step-by-Step Solution
Solution:
  1. Step 1: Understand inheritance of exceptions

    EOFError is a subclass of IOError.
  2. Step 2: Match exception with rescue clauses

    Rescue clauses match exception types and their subclasses; so IOError rescue catches EOFError.
  3. Final Answer:

    The IOError rescue clause handles the EOFError. -> Option C
  4. Quick Check:

    Subclass exceptions caught by parent rescue [OK]
Quick Trick: Rescue matches exception and subclasses [OK]
Common Mistakes:
  • Assuming subclass exceptions go to generic rescue
  • Thinking both rescues run
  • Believing exception is uncaught

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes