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?
begin
  raise ArgumentError, 'wrong argument'
rescue ZeroDivisionError
  puts 'Zero division error caught'
rescue ArgumentError
  puts 'Argument error caught'
end
AZero division error caught
BNo output, program crashes
CArgument error caught
DRuntime error: unhandled exception
Step-by-Step Solution
Solution:
  1. Step 1: Identify the raised exception

    The code raises an ArgumentError with message 'wrong argument'.
  2. Step 2: Match the raised exception with rescue clauses

    The first rescue handles ZeroDivisionError which does not match; the second rescue handles ArgumentError which matches and runs.
  3. Final Answer:

    The output is 'Argument error caught'. -> Option C
  4. Quick Check:

    Raised exception matches second rescue clause [OK]
Quick Trick: Raised exception triggers first matching rescue clause [OK]
Common Mistakes:
  • Assuming first rescue always runs
  • Confusing exception types
  • Expecting program crash without rescue

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes