raise
already sets the message so you don't have to pass it to the constructor:
class MyCustomError < StandardError attr_reader :object def initialize(object) @object = object endendbegin raise MyCustomError.new("an object"), "a message"rescue MyCustomError => e puts e.message # => "a message" puts e.object # => "an object"end
I've replaced rescue Exception
with rescue MyCustomError
, see Why is it a bad style to `rescue Exception => e` in Ruby?.