Ruby Custom Error Handling

August 07, 2019

Here is an example of how to create your own custom error:

class Example
  class CustomError < StandardError
    def message
      'I am a custom error'
    end
  end

  class << self
    def call
      raise CustomError
    end
  end
end

how to use it:

begin
  Example.call
rescue Example::CustomError => e
  puts e.message
end

Search