Bird
0
0

Find the error in this Ruby code snippet:

medium📝 Debug Q14 of 15
Ruby - Error Handling
Find the error in this Ruby code snippet:
begin
  puts 5 + "3"
rescue
  puts "Error caught"
end
AThe begin block should be replaced with try
BThe rescue block is missing the error type
CYou cannot add a number and a string directly
DThe code will run without errors
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the operation inside begin block

    Adding 5 (number) and "3" (string) causes a TypeError in Ruby.
  2. Step 2: Check error handling

    The rescue block catches any error and prints "Error caught"; the error is due to incompatible types.
  3. Final Answer:

    You cannot add a number and a string directly -> Option C
  4. Quick Check:

    Type mismatch causes error caught [OK]
Quick Trick: Check data types before adding values [OK]
Common Mistakes:
  • Thinking rescue needs error type always
  • Assuming code runs without error
  • Confusing begin with try

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes