Bird
0
0

Given this code, what will be printed?

hard📝 Application Q9 of 15
Ruby - Error Handling
Given this code, what will be printed?
begin
  raise NoMethodError, 'undefined method'
rescue StandardError => e
  puts e.class
rescue NoMethodError
  puts 'NoMethodError rescued'
end
ANoMethodError rescued
BNoMethodError
CStandardError
DRuntimeError
Step-by-Step Solution
Solution:
  1. Step 1: Identify raised exception

    The code raises NoMethodError.
  2. Step 2: Understand rescue order

    The first rescue catches StandardError and subclasses, including NoMethodError. So it handles the exception and prints its class.
  3. Step 3: Second rescue is unreachable

    Because the first rescue catches the exception, the second rescue is never reached.
  4. Final Answer:

    NoMethodError -> Option B
  5. Quick Check:

    First matching rescue handles exception [OK]
Quick Trick: First matching rescue block handles the exception [OK]
Common Mistakes:
  • Assuming second rescue runs after first
  • Confusing rescue order
  • Thinking NoMethodError rescue runs first

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes