Elegant way to silently ignore a Ruby exception

I was working on a small hobby project and wanted to ignore some of the Exceptions that were raised.

The first version of the code that I wrote was something similar to:

def ignore_exception  
   rescue Exception

and using

ignore_exception { puts "Ignoring Exception"; raise Exception; puts "This is Ignored" }  

After a bit of more googling and research, I came across another clean solution provided by ActiveSupport

suppress(Exception) do  
   # dangerous code here

PS: Don't ask me why I wanted to do this, as I mentioned this was just a hobby project and will/should never go out into a production code.

Ref: http://api.rubyonrails.org/classes/Kernel.html#method-i-suppress

comments powered by Disqus