ruby picture

RCR 272: ignoring raise exceptions (supressing all errors)

Submitted by mig (Wed Aug 04 05:08:52 UTC 2004)

Abstract

I have method with many "raise". I want to call it and herewith supress any errors (ignore these raises).

Problem

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

Proposal

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.

Analysis

Implementation

ruby picture
Comments Current voting

I don't think I understand the proposal.

  • How do you turn off raise?
  • How do you care serious breakage caused by ignoring exceptions?

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


But you can retry too and that's not a "serious problem". This RCR is essentially resume --and for that I vote yes. I see no real reason retry is okay but not resume. --T.


Resuming is interesting topic to discuss. But I think it's separate issue. Why don't you submit another RCR if you have resuming exception idea.

-- matz.


Strongly opposed 5
Opposed 0
Neutral 0
In favor 0
Strongly advocate 0
ruby picture

This RCR supersedes RCR 271.

If you have registered at RCRchive, you may now sign in below. If you have not registered, you may sign up for a username and password. Registering enables you to submit new RCRs, and vote and leave comments on existing RCRs.
Your username:
Your password:

ruby picture

Powered by .