RuntimeError
RubyERRORNotableRuntime

Generic runtime error

Quick Answer

Replace generic RuntimeError raises with a descriptive custom error class for easier debugging and handling.

What this means

RuntimeError is the default exception raised by raise when no class is specified. It is a direct subclass of StandardError and is used for miscellaneous runtime failures that do not fit a more specific error class.

Why it happens
  1. 1Calling raise "message" or raise without specifying a class
  2. 2Application logic detecting an invalid state with no specific error type available

Fix

Use a named custom error

Use a named custom error
class InvalidStateError < StandardError; end

def process(order)
  raise InvalidStateError, "order #{order.id} is already closed" if order.closed?
  # ...
end

Why this works

Named errors give callers precise rescue targets and produce cleaner backtraces.

Code examples
Default raise produces RuntimeErrorruby
raise "something failed"
# RuntimeError: something failed
Explicit RuntimeErrorruby
raise RuntimeError, "explicit message"
Rescuing itruby
begin
  raise "oops"
rescue RuntimeError => e
  puts e.message   # oops
end
Sources
Official documentation ↗

Ruby Core Documentation

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

← All Ruby errors