From time to time I see people write code and at the end of the line there will be: rescue nil. This is not good because it will rescue from any exception.

I see people rescuing nil because they have a variable and they aren’t sure if it is nil so they want to protect against running a method on it which would cause an error. The two most common rescue nil scenarios I see people protect against are:

  • a parameter not being present
  • some method being called on a variable that may represent an object or may be nil

Below is an example of rescue nil that I see often. The rescue nil on line 13 is there to protect against a category not being present. This rescue will also handle an exception due to the caching system not being available. If you have any notification systems in place such as Airbrake or exception_notification you aren’t going to hear about your caching system system being down. If there is some other error in the ask_question method you also are not going to hear about it because rescue nil will eat everything up.

A better solution is to just check and see if the category is actually present before running a method on it.

Thanks for reading, Let me know your thoughts.