Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to "ignore" caught exceptions?

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?

like image 955
Radek Avatar asked Nov 25 '12 22:11

Radek


People also ask

How do I ignore an exception?

There is no way to basically ignore a thrown exception. The best that you can do is to limit the standard you have to wrap the exception-throwing code in.

How do you handle catching exception?

The first catch block that handles the exception class or one of its superclasses will be executed. So, make sure to catch the most specific class first. If an exception occurs in the try block, the exception is thrown to the first catch block. If not, the Java exception passes down to the second catch statement.

How do I ignore a specific exception in Python?

Using Try Exceptexcept ... block to catch the ZeroDivisionError exception and ignore it. In the above code, we catch the ZeroDivisionError exception and use pass to ignore it. So, when this exception happens, nothing will be thrown and the program will just keep running by ignoring the zero number.

How do you handle exceptions without catching and catching?

throws: The throws keyword is used for exception handling without try & catch block. It specifies the exceptions that a method can throw to the caller and does not handle itself.


2 Answers

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
like image 157
Sergio Tulentsev Avatar answered Oct 06 '22 14:10

Sergio Tulentsev


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.

like image 29
vgoff Avatar answered Oct 06 '22 15:10

vgoff