Skip to content

Instantly share code, notes, and snippets.

@nick-desteffen
Created March 25, 2012 15:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nick-desteffen/2197440 to your computer and use it in GitHub Desktop.
Save nick-desteffen/2197440 to your computer and use it in GitHub Desktop.
Why rescue nil is bad - fixed
class Category < ActiveRecord::Base
def ask_question(question)
begin
## Code to check some other system for the answer, Redis or Memcached maybe.
raise "System is unavailable, you should turn it on"
rescue Exception => e
## Do something with exception
puts "System is unavailable"
return nil
end
end
end
params = {category_id: 1, question: "Why did the quick brown fox jump over the lazy dog?"}
if category && answer = category.ask_question(params[:question])
## Do something with the answer
else
## Do something else
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment