Bird
0
0

Which of the following Ruby code snippets correctly uses the rescue modifier inline to handle exceptions?

easy📝 Syntax Q3 of 15
Ruby - Error Handling
Which of the following Ruby code snippets correctly uses the rescue modifier inline to handle exceptions?
Aresult = rescue 0 100 / x
Bresult = 100 / x rescue 0
Cresult = 100 / x then rescue 0
Dresult = 100 / x else rescue 0
Step-by-Step Solution
Solution:
  1. Step 1: Understand rescue modifier syntax

    The rescue modifier is used inline after an expression to catch exceptions and provide a fallback value.
  2. Step 2: Analyze each option

    result = 100 / x rescue 0 correctly places rescue 0 after the expression 100 / x. Options B, C, and D use incorrect syntax.
  3. Final Answer:

    result = 100 / x rescue 0 -> Option B
  4. Quick Check:

    Check that rescue follows the expression directly [OK]
Quick Trick: Rescue modifier follows expression directly [OK]
Common Mistakes:
  • Placing rescue before the expression
  • Using 'then' or 'else' with rescue modifier inline
  • Incorrect order of rescue and expression

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes