It's easy to create your own exceptions in Ruby. Just follow these steps:

1. Make a New Class

Exceptions are classes, just like everything else in Ruby! To create a new kind of exception, just create a class that inherits from StandardError, or one of its children.

class MyError < StandardError
end

raise MyError

By convention, new exceptions have class names ending in "Error". It's also good practice to put your custom exceptions inside a module. That means your final error classes will look like this: ActiveRecord::RecordNotFound and Net::HTTP::ConnectionError.

2. Add a message

Every ruby exception object has a message attribute. It's the longer bit of text that's printed out next to the exception name

Example of an exception's message attribute Example of an exception's message attribute

You can specify a message when you raise an exception, like so:

raise MyError, "My message"

And you can add a default message to your custom error class by adding your own constructor.

class MyError < StandardError
  def initialize(msg="My default message")
    super
  end
end

3. Add a custom data attributes to your exception

You can add custom data to your exception just like you'd do it in any other class. Let's add an attribute reader to our class and update the constructor.

class MyError < StandardError
  attr_reader :thing
  def initialize(msg="My default message", thing="apple")
    @thing = thing
    super(msg)
  end
end

begin
  raise MyError.new("my message", "my thing")
rescue => e
  puts e.thing # "my thing"
end

That's it! Creating a custom error or exception in Ruby really isn't that difficult. There is one thing to be aware of. Notice how we always inherited from StandardError in the examples? That's intentional. While there is an Exception class in Ruby, you should NEVER EVER inherit directly from it. For more information about this, check out our article about the differences between Exception and StandardError

Get the Honeybadger newsletter

Each month we share news, best practices, and stories from the DevOps & monitoring community—exclusively for developers like you.
    author photo
    Starr Horne

    Starr Horne is a Rubyist and Chief JavaScripter at Honeybadger.io. When she's not neck-deep in other people's bugs, she enjoys making furniture with traditional hand-tools, reading history and brewing beer in her garage in Seattle.

    More articles by Starr Horne
    An advertisement for Honeybadger that reads 'Turn your logs into events.'

    "Splunk-like querying without having to sell my kidneys? nice"

    That’s a direct quote from someone who just saw Honeybadger Insights. It’s a bit like Papertrail or DataDog—but with just the good parts and a reasonable price tag.

    Best of all, Insights logging is available on our free tier as part of a comprehensive monitoring suite including error tracking, uptime monitoring, status pages, and more.

    Start logging for FREE
    Simple 5-minute setup — No credit card required