QuickSnip LogoA light blue logo with a thunderbolt in bracketsQuickSnip

Custom Error Class

Defines and raises a custom error class in Ruby.

  • error handling
  • custom error

Code

class MyCustomError < StandardError; end

def risky_method(value)
    raise MyCustomError, "Value must be positive" if value <= 0
    "Valid value: #{value}"
end

Usage

begin
    puts risky_method(-1)
rescue MyCustomError => e
    puts e.message # Output: "Value must be positive"
end