โ Ruby Programming learning pathRuby Programming ยท Module 8 of 10
Errors and exceptions
begin/rescue handles errors so programs fail gracefully.
๐ Key ideas
- begin ... rescue ... end catches exceptions.
- Rescue specific errors: rescue ZeroDivisionError => e.
- ensure runs whether or not an error happened.
- raise ArgumentError, "marks must be 0-100" signals a problem.
- 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