liveBook · Manning

StandardError

As explained in The Well-Grounded Rubyist, Fourth Edition

StandardError is Ruby's class for the standard group of application-level exceptions that commonly inherit from it.

StandardError is the superclass for Ruby's common application-level exceptions. An unqualified rescue handles exceptions descended from StandardError, while more specific rescue clauses limit what gets caught. section 6.4.2 section 6.4.1

The class sits below Exception in Ruby's exception hierarchy. Common descendants include ZeroDivisionError, RuntimeError, NoMethodError, NameError, IOError, TypeError, and ArgumentError. section 6.4.1

Use StandardError deliberately when handling or defining exceptions:

  • Use an unqualified rescue when the surrounding operation should handle ordinary application errors. section 6.4.2
  • Name a specific exception, such as ZeroDivisionError, to avoid hiding unrelated failures. section 6.4.2
  • Inherit custom exceptions from StandardError so ordinary rescue handles them while their names communicate precise meaning. section 6.4.8
  • Namespace custom exception classes when a program defines many of them or needs to avoid name clashes. section 6.4.8

A custom application exception

class InvalidLineError < StandardError end raise InvalidLineError, "some new kind of error has occurred!" section 6.4.8

class MyNewException < StandardError
end
raise MyNewException, "some new kind of error has occurred!"

Inspect a minimal custom exception declaration and raise operation. section 6.4.8

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Explains the Exception hierarchy, rescue matching, specific handling, custom subclasses, and namespacing without surveying exception internals.
Examples
Uses division by zero, common exception listings, and custom InvalidLineError declarations to demonstrate the hierarchy.
What it assumes
Assumes familiarity with Ruby classes and rescue while introducing exception inheritance through concrete division and file-processing examples.
What you can do after
Build custom application exceptions and apply precise rescue clauses without masking unrelated failures.
How it connects
Connects StandardError with Exception, rescue, exception classes, ZeroDivisionError, and custom exception design.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Rubyist, Fourth Edition. The section links point to the parts of the book it draws on.