/ ruby

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
   begin
     yield  
   rescue Exception
   end
end

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
end

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

Manu S Ajith

Manu S Ajith

Tech Entrepreneur, dating Elixir, in long-term ❤️ w/ Ruby, had multiple one night stands w/ Go. Into functional paradigms DDD/CQRS/EventSourcing architecture these days. @manusajith on the interwebs

Read More