# Rescue and principle of least surprise

**URL:** <https://www.ruby-forum.com/t/rescue-and-principle-of-least-surprise/160502>\
**Category:** Ruby\
**Created:** [February 17, 2009, 5:22am UTC](https://www.ruby-forum.com/t/rescue-and-principle-of-least-surprise/160502 "2009-02-17T05:22:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![matt\_neuburg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@matt\_neuburg](https://www.ruby-forum.com/u/matt_neuburg)\
**Post date:** [February 17, 2009, 5:22am UTC](https://www.ruby-forum.com/t/rescue-and-principle-of-least-surprise/160502/1 "2009-02-17T05:22:27Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![matt\_neuburg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@matt\_neuburg](https://www.ruby-forum.com/u/matt_neuburg)\
**Post date:** [February 17, 2009, 2:13pm UTC](https://www.ruby-forum.com/t/rescue-and-principle-of-least-surprise/160502/2 "2009-02-17T14:13:30Z")

</div>

On Mon, Feb 16, 2009 at 11:21 PM, matt neuburg [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)
