I'm looking for something like this:
raise Exception rescue nil
But the shortest way I've found is this:
begin raise Exception rescue Exception end
To ignore additional errors, use the exceptions. ignore configuration option. The gem will ignore any exceptions matching the string, regex or class that you add to exceptions.
A raised exception can be rescued to prevent it from crashing your application once it reaches the top of the call stack. In Ruby, we use the rescue keyword for that. When rescuing an exception in Ruby, you can specify a specific error class that should be rescued from.
To ignore an exception in Java, you need to add the try... catch block to the code that can throw an exception, but you don't need to write anything inside the catch block.
Ruby provide a nice mechanism to handle exceptions. We enclose the code that could raise an exception in a begin/end block and use rescue clauses to tell Ruby the types of exceptions we want to handle.
This is provided by ActiveSupport:
suppress(Exception) do # dangerous code here end
http://api.rubyonrails.org/classes/Kernel.html#method-i-suppress
def ignore_exception begin yield rescue Exception end end
Now write you code as
ignore_exception { puts "Ignoring Exception"; raise Exception; puts "This is Ignored" }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With