I can't seem to find much information about custom exception classes.
What I do know
You can declare your custom error class and let it inherit from StandardError
, so it can be rescue
d:
class MyCustomError < StandardError
end
This allows you to raise it using:
raise MyCustomError, "A message"
and later, get that message when rescuing
rescue MyCustomError => e
puts e.message # => "A message"
What I don't know
I want to give my exception some custom fields, but I want to inherit the message
attribute from the parent class. I found out reading on this topic that @message
is not an instance variable of the exception class, so I'm worried that my inheritance won't work.
Can anyone give me more details to this? How would I implement a custom error class with an object
attribute? Is the following correct:
class MyCustomError < StandardError
attr_reader :object
def initialize(message, object)
super(message)
@object = object
end
end
And then:
raise MyCustomError.new(anObject), "A message"
to get:
rescue MyCustomError => e
puts e.message # => "A message"
puts e.object # => anObject
will it work, and if it does, is this the correct way of doing things?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…