I have method with many "raise". I want to call it and herewith supress any errors (ignore these raises).
It would be useful when you have a method which is called at once by user and internally. If internally, you need to supress testings and force it.
def foo raise "account already exist" if bla raise "account..." if bla create_account end
Nowadays I must do it like this (which makes code less readable and larger):
def foo
unless @force
raise "account already exist" if bla
raise "account..." if bla
end
create_account
end
I think this could be an elegant solution:
begin my_method rescue ignore end
Ignore would act as continue of raise, you'll be able to do some stuff and then return back.
The latter is more important. Since most code do not expect raise not terminating the method execution, ignoring error can cause serious problem. In general, I feel like this is not useful except for special cases like yours, unless I misunderstand you.
--matz.
I've think about it and you're right. If I really need it I can write "myraise" wrapper and force method:
def myraise text
unless @force
raise text
end
end
def force
@force = true yield @force = false
end
Back to RCRchive.
RCR Submission page and RCRchive powered by Ruby, Apache, RuWiki (modified), and RubLog