How to "ignore" detected exceptions?

I use rufus scheduler to run nightly test scripts, calling my functions.

Sometimes I see a “scheduler caused exception”: a message that threw some of my functions. The scheduler then stops the following test cases.

How can I do this so that the scheduler runs all the test cases, no matter what exception is thrown?

+4
source share
2 answers

This is called "swallowing exclusion." You catch the exception and do nothing with it.

begin # do some dangerous stuff, like running test scripts rescue => ex # do nothing here, except for logging, maybe end 

If you do not need to do anything with an exception, you can omit => ex :

 begin # do some dangerous stuff, like running test scripts rescue; end 

If you need to save Exceptions that are not a subclass of StandardError , you need to be more explicit:

 begin # do some dangerous stuff, like running test scripts rescue Exception # catches EVERY exception end 
+8
source

I sometimes use the fact that you can pass blocks to a method, and I have method recovery errors, and my code can continue on its way.

 def check_block yield rescue NoMethodError => e <<-EOR Error raised with message "#{e}". Backtrace would be #{e.backtrace.join('')} EOR end puts check_block {"some string".sort.inspect} puts check_block {['some', 'array'].sort.inspect} 

This first block will pass and save the returned report, the second will work fine.

This salvation saves only NoMethodError , while you may need to save other errors.

+2
source

All Articles