Bird
0
0

Why is it recommended to raise errors inheriting from StandardError instead of Exception in Ruby?

hard📝 Conceptual Q10 of 15
Ruby - Error Handling
Why is it recommended to raise errors inheriting from StandardError instead of Exception in Ruby?
ABecause Exception is not a valid error class
BBecause rescue without arguments only catches StandardError and its subclasses
CBecause StandardError errors are faster to raise
DBecause Exception errors cannot have messages
Step-by-Step Solution
Solution:
  1. Step 1: Understand Ruby's rescue behavior

    By default, rescue catches only StandardError and its subclasses, not all Exceptions.
  2. Step 2: Reason why inheriting StandardError is preferred

    Raising errors inheriting from StandardError ensures they are caught by normal rescue blocks.
  3. Final Answer:

    Because rescue without arguments only catches StandardError and its subclasses -> Option B
  4. Quick Check:

    Rescue catches StandardError subclasses = B [OK]
Quick Trick: Rescue catches StandardError, not all Exception subclasses [OK]
Common Mistakes:
  • Thinking Exception is invalid
  • Believing StandardError is faster
  • Assuming Exception can't have messages

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes