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
