Custom Exceptions in Ruby: Classes, Messages & Hierarchies
To create a custom exception in Ruby, define a class that inherits from StandardError: class PaymentError < StandardError; end. Raise it with raise PaymentError, "card declined" and rescue it by class. Inherit from StandardError, not Exception, so a bare rescue still catches it. Pass a default message by overriding initialize and calling super. Ruby’s built-in exception classes describe the language’s failure modes, not your application’s.