Bird
0
0

Which of the following is true about the begin/rescue/end block in Ruby?

easy📝 Conceptual Q2 of 15
Ruby - Error Handling
Which of the following is true about the begin/rescue/end block in Ruby?
ACode inside <code>begin</code> runs only if no exception occurs
BThe <code>rescue</code> block runs before the <code>begin</code> block
CCode inside <code>rescue</code> runs only if an exception occurs
DThe <code>end</code> keyword is optional
Step-by-Step Solution
Solution:
  1. Step 1: Identify when rescue runs

    The rescue block executes only if an exception is raised inside the begin block.
  2. Step 2: Check other options

    Code inside begin always runs first, end is mandatory, and rescue does not run before begin.
  3. Final Answer:

    Code inside rescue runs only if an exception occurs -> Option C
  4. Quick Check:

    Rescue runs on error = B [OK]
Quick Trick: Rescue runs only when errors happen inside begin [OK]
Common Mistakes:
  • Thinking rescue runs always
  • Believing end is optional
  • Confusing execution order

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes