Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Correct some erreoneous information about ruby execption handling. #91

Merged
merged 1 commit into from

2 participants

@samg
begin; rescue Exception; end

is not the same as

begin: rescue; end # this rescues from StandardError, not Exception

This former is a bad practice which results in processes that don't
respond to kill and other signals. It's rarely what's intended.

Try this one for fun:

begin
  # calls to exit and kill signals will be caught (except kill -9)
  exit
rescue Exception
  puts "you didn't really want to exit, right?"
  # exception handling
end 
Sam Goldstein Correct some errors regarding `rescue Exception`
begin; rescue Exception; end

is not the same as

begin: rescue; end

This former is a bad practice which results in processes that don't
respond to `kill` and other signals.  It's rarely what's intended.
1b0fd8f
@bbatsov bbatsov merged commit f9e8dd6 into from
@bbatsov
Owner

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 6, 2012
  1. Correct some errors regarding `rescue Exception`

    Sam Goldstein authored
    begin; rescue Exception; end
    
    is not the same as
    
    begin: rescue; end
    
    This former is a bad practice which results in processes that don't
    respond to `kill` and other signals.  It's rarely what's intended.
This page is out of date. Refresh to see the latest.
Showing with 19 additions and 6 deletions.
  1. +19 −6 README.md
View
25 README.md
@@ -857,22 +857,35 @@ in *Ruby* now, not in *Python*.
n / d
```
-* Avoid rescuing the `Exception` class.
+* Avoid rescuing the `Exception` class. This will trap signals and calls to
+ `exit`, requiring you to `kill -9` the process.
```Ruby
- # bad
+ # bad
begin
- # an exception occurs here
- rescue
+ # calls to exit and kill signals will be caught (except kill -9)
+ exit
+ rescue Exception
+ puts "you didn't really want to exit, right?"
# exception handling
end
- # still bad
+ # good
+ begin
+ # a blind rescue rescues from StandardError, not Exception as many
+ # programmers assume.
+ rescue => e
+ # exception handling
+ end
+
+ # also good
begin
# an exception occurs here
- rescue Exception
+
+ rescue StandardError => e
# exception handling
end
+
```
* Put more specific exceptions higher up the rescue chain, otherwise
Something went wrong with that request. Please try again.