Skip to content
Browse files

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.
  • Loading branch information...
1 parent 95f7e7e commit 1b0fd8f6b602babc992d25b6edd7753cdbb26e27 Sam Goldstein committed Apr 5, 2012
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

0 comments on commit 1b0fd8f

Please sign in to comment.
Something went wrong with that request. Please try again.