How to raise custom exceptions in Ruby

July 18, 2022  ‐ 1 min read

You can get quite far with throwing generic exceptions, by just passing a string to raise.

raise 'There was a timeout'

But, creating your own Exception hierarchies allows you to be more specific in catching and handling them.

All custom errors classes you define should inherit from StandardError or one of its subclasses.

class HttpTimeoutError < StandardError

raise HttpTimeoutError