Junior Track Roadmap

Ruby exception handling

This course covers exception handling in Ruby. It starts with raise and the exception object, an instance of a class with its own class and message. It then covers begin and rescue for catching a specific class, the order of several rescue clauses, and the bare rescue boundary at StandardError. Later lessons explain else and ensure, custom exception classes and hierarchies, and rescuing Rails exceptions such as ActiveRecord::RecordNotFound and ActiveRecord::RecordInvalid by name. A final method combines every piece.

What you'll learn

  • ✓How raise stops execution and unwinds through calling methods
  • ✓Catching a specific exception class with begin and rescue
  • ✓Why a bare rescue stops at StandardError and skips SignalException and SystemExit
  • ✓Using else for clean runs and ensure for cleanup on every path
  • ✓Defining custom exception classes and hierarchies
  • ✓Rescuing Rails exceptions by name and reading e.record and e.param
Free preview · Lesson 1 3 min read

raise and the exception object

In an earlier course, code you wrote raised ActiveRecord::RecordNotFound or ActiveModel::ForbiddenAttributesError. The page broke, a stack trace showed up, and the fix was to change the code so the error stopped happening. This course is about what happens in that moment, and how to take control of…

Read the full lesson free →

Curriculum

7 lessons
Type to start searching...