Is there any way in Ruby to have a standard rescue strategy based in inheritance?
I'd like to have something like:
class Parent
def method
rescue StandardError => e
#handle error
end
end
class Child < Parent
def method
raise StandardError.new("ERROR") if error_present
#do some stuff
super
end
def error_present
#checks for errors
end
end
and the result I expect is that when the StandardError is raised in the child's method it will be rescued from the parent's method definition.
If that's not possible, is there any other way I could achieve this behaviour?
I am afraid this is not possible - one method's
rescuecan only rescue errors raised within its body.One way to deal with this would be to offer another method to override in subclasses:
That being said - please do not rescue
StandardError. It will make your future debugging a nightmare. Create your own error subclass instead.