โ† Ruby Programming learning path
Ruby Programming ยท Module 8 of 10

Errors and exceptions

begin/rescue handles errors so programs fail gracefully.

๐Ÿ“– Key ideas

  1. begin ... rescue ... end catches exceptions.
  2. Rescue specific errors: rescue ZeroDivisionError => e.
  3. ensure runs whether or not an error happened.
  4. raise ArgumentError, "marks must be 0-100" signals a problem.
  5. Define custom errors with class MarksError < StandardError; end.
๐Ÿ’ก Real-life example: Like a safety net under a tightrope walker: rescue catches the fall, ensure folds the net away afterwards.
rescueCatches an exception
ensureAlways runs at the end
raiseSignals an error
StandardErrorBase class for normal errors