14

I use rufus scheduler to run overnight test scripts by calling my functions.

Sometimes I can see "scheduler caught exception:" a message that threw some of my functions. Then scheduler stops execution of following test cases.

How can I make it so scheduler runs all test cases regardless of any exception caught?

Radek
  • 13,813
  • 52
  • 161
  • 255

2 Answers2

17

This is called "exception swallowing". You intercept an exception and don't do anything 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 the exception, you can omit the => ex:

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

If you need to rescue Exceptions that don't subclass from StandardError, you need to be more explicit:

begin
  # do some dangerous stuff, like running test scripts
rescue Exception
  # catches EVERY exception
end
Community
  • 1
  • 1
Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
2

I will sometimes use the fact that you can pass blocks to a method, and I have the method rescue 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 go through and rescue with a report returned, the second will operate normally.

This rescue only rescues NoMethodError while you may need to rescue other errors.

vgoff
  • 10,980
  • 3
  • 38
  • 56
  • Hey, quick question, I'm fairly new to Ruby and have never seen the <<-EOR stuff. Can you explain briefly what that does? Thanks! – BFree Nov 25 '12 at 23:33
  • 1
    Sure. It is a HereDoc syntax. It basically let me use the quotes in the string without needing to escape them. Which in this case probably hinted that the double quotes were needed for the string interpolation. It is not. The HereDoc already is a double quoted string syntax. The dash says that I can have the terminator indented from 0 column. The EOR delimiter is arbitrary. Could be just about anything. – vgoff Nov 25 '12 at 23:36
  • With your explanation, along with wikipedia and messing around with it, I get it all now. Thank you so much! – BFree Nov 26 '12 at 00:29
  • In this [answer](http://stackoverflow.com/questions/13359746/clean-way-to-build-long-strings-in-ruby/13362128#13362128) I show how to use a method directly on a HereDoc. – vgoff Nov 26 '12 at 10:07