rubyseleniumexception

Wrap each command in a rescue statement


I have 10 Ruby function calls I'd like to execute and each call could possibly throw an exception. I'd like to handle each exception the same way and continue. Is there a way to do this without wrapping each line in a begin ... rescue ... end block?

[Edit]: Use case for this is a screen scraper/automation tool that uses the Selenium web driver to fill out forms. I don't want to bother checking if options in select elements exist, just fill them out as good as possible. for this, I need to call Selenium::WebDriver::Support::Select.select_by and continue if it throws a "cannot locate option with value x" exception.


Solution

  • You mean something like this ?

    class Wtf
      def f1
        raise "f1"
      end
      def f2
        raise "f2"
      end
    end
    
    obj= Wtf.new
    
    [:f1, :f2].each do |f|
      begin
        obj.send f
      rescue Exception=> e
        p e
      end
    end
    

    Edit: added more code to the example