Bird
0
0

What does the rescue modifier do in Ruby when used like value = 10 / x rescue 0?

easy📝 Conceptual Q11 of 15
Ruby - Error Handling
What does the rescue modifier do in Ruby when used like value = 10 / x rescue 0?
AIt ignores the division and returns nil
BIt always returns 10 divided by x
CIt returns 0 if dividing by x causes an error
DIt raises an error if x is zero
Step-by-Step Solution
Solution:
  1. Step 1: Understand the rescue modifier usage

    The rescue modifier catches exceptions raised by the expression before it and returns the value after rescue if an error occurs.
  2. Step 2: Apply to the example

    If 10 / x raises an error (like division by zero), the rescue modifier returns 0 instead of crashing.
  3. Final Answer:

    It returns 0 if dividing by x causes an error -> Option C
  4. Quick Check:

    rescue modifier returns fallback on error [OK]
Quick Trick: Rescue modifier returns fallback if error happens [OK]
Common Mistakes:
  • Thinking rescue modifier prevents all errors
  • Assuming it always returns the original expression
  • Confusing rescue modifier with if-else

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes