Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > rescue and principle of least surprise

Reply
Thread Tools

rescue and principle of least surprise

 
 
matt neuburg
Guest
Posts: n/a
 
      02-17-2009
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.velocityreviews.com/forums/(E-Mail Removed), http://www.tidbits.com/matt/
Leopard - http://www.takecontrolbooks.com/leop...stomizing.html
AppleScript - http://www.amazon.com/gp/product/0596102119
Read TidBITS! It's free and smart. http://www.tidbits.com
 
Reply With Quote
 
 
 
 
Rick DeNatale
Guest
Posts: n/a
 
      02-17-2009
[Note: parts of this message were removed to make it a legal post.]

On Mon, Feb 16, 2009 at 11:21 PM, matt neuburg <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Conventions in ruby and the principle of least surprise Einar Boson Ruby 6 02-02-2009 04:44 PM
Need regular expression for at least 7 characters and at least 1 special chatacter AAaron123 ASP .Net 0 10-03-2008 01:25 PM
Principle of le,err i mean biggest surprise!!?? globalrev Ruby 4 05-10-2008 12:12 PM
Quiz #65, Principle of Great Surprise, and Array.delete sledgehammer Dave Howell Ruby 20 02-05-2006 09:09 PM
Kernel.y and yes,yes,yes not least surprise Jamie Herre Ruby 1 01-07-2005 07:33 PM



Advertisments