rescue and principle of least surprise

Discussion in 'Ruby' started by matt neuburg, Feb 17, 2009.

  1. matt neuburg

    matt neuburg Guest

    It came as a big surprise to me when I discovered that this code:

    begin
    # do stuff
    rescue
    # do rescue stuff
    end

    ....could fail to catch exceptions thrown in the "do stuff" section. A
    bare "rescue" looks to me like it ought to mean: catch every exception.
    Instead it turns out to mean: catch a certain subset of exceptions. It
    doesn't catch LoadError, it doesn't catch SyntaxError, etc. etc.

    Of course now I know better (and I commonly write "rescue Exception"),
    but it still feels wrong, especially in view of the "principle of least
    surprise". And I see by a quick Google search that people regard this as
    an annoying "gotcha". Is it worth proposing an actual change in the
    language - that the default rescue be Exception instead of
    StandardError? m.

    --
    matt neuburg, phd = , http://www.tidbits.com/matt/
    Leopard - http://www.takecontrolbooks.com/leopard-customizing.html
    AppleScript - http://www.amazon.com/gp/product/0596102119
    Read TidBITS! It's free and smart. http://www.tidbits.com
     
    matt neuburg, Feb 17, 2009
    #1
    1. Advertising

  2. [Note: parts of this message were removed to make it a legal post.]

    On Mon, Feb 16, 2009 at 11:21 PM, matt neuburg <> wrote:

    > It came as a big surprise to me when I discovered that this code:
    >
    > begin
    > # do stuff
    > rescue
    > # do rescue stuff
    > end
    >
    > ...could fail to catch exceptions thrown in the "do stuff" section. A
    > bare "rescue" looks to me like it ought to mean: catch every exception.
    > Instead it turns out to mean: catch a certain subset of exceptions. It
    > doesn't catch LoadError, it doesn't catch SyntaxError, etc. etc.
    >
    > Of course now I know better (and I commonly write "rescue Exception"),
    > but it still feels wrong, especially in view of the "principle of least
    > surprise". And I see by a quick Google search that people regard this as
    > an annoying "gotcha". Is it worth proposing an actual change in the
    > language - that the default rescue be Exception instead of
    > StandardError? m.
    >


    No, I don't think so. The distinction between StandardErrors and the others
    is carefully thought out, and makes sense.

    Exceptions which are not StandardErrors representation situations which are
    normally not handled by a 'normal' Ruby program, either because it's usually
    better to handle it the standard way (e.g. a SignalException because someone
    issued a kill command, or a SystemExit exception), or difficult for the
    program to recover from (e.g. ScriptErrors, and NoMemoryErrors).

    The fact that you can explicitly rescue these exceptional exceptions means
    that you don't HAVE to let them be handled in the standard way, but you
    still have to carefully consider how that rescue clause needs to be written.

    Also, if feel the need to rescue one or more of these low level Exceptions,
    I it's usually wise to rescue them specifically rather than rescuing
    Exception in general.

    --
    Rick DeNatale

    Blog: http://talklikeaduck.denhaven2.com/
    Twitter: http://twitter.com/RickDeNatale
     
    Rick DeNatale, Feb 17, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. AAaron123
    Replies:
    0
    Views:
    648
    AAaron123
    Oct 3, 2008
  2. Jamie Herre
    Replies:
    1
    Views:
    227
    why the lucky stiff
    Jan 7, 2005
  3. Dave Howell
    Replies:
    20
    Views:
    280
    William James
    Feb 5, 2006
  4. globalrev
    Replies:
    4
    Views:
    115
    Marc Heiler
    May 10, 2008
  5. Einar Boson
    Replies:
    6
    Views:
    202
    Rick DeNatale
    Feb 2, 2009
Loading...

Share This Page