StandardError
RubyERRORNotableCore

Base class for most rescuable errors

Quick Answer

Use rescue StandardError (or bare rescue) to catch all typical application errors without suppressing system signals.

What this means

StandardError is the parent of almost all errors that application code should handle. A bare rescue clause (rescue without specifying a class) catches StandardError and its descendants. It sits one level below Exception, excluding fatal signals.

Why it happens
  1. 1Bare rescue blocks catching more than intended when multiple error types can be raised
  2. 2Custom exception classes not inheriting from StandardError, making them uncatchable by default rescue

Fix

Define custom errors from StandardError

Define custom errors from StandardError
class MyAppError < StandardError
  def initialize(msg = 'application error occurred')
    super
  end
end

raise MyAppError, 'something went wrong'

Why this works

Inheriting from StandardError ensures your custom error is caught by bare rescue and by rescue StandardError.

Code examples
Bare rescue catches StandardErrorruby
begin
  raise "oops"
rescue => e          # same as rescue StandardError
  puts e.class       # RuntimeError
  puts e.message     # oops
end
Custom error subclassruby
class PaymentError < StandardError; end
raise PaymentError, 'card declined'
Checking ancestryruby
ArgumentError < StandardError  # => true
Interrupt < StandardError       # => false
Same error in other languages
Sources
Official documentation ↗

Ruby Core Documentation

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All Ruby errors