Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 874
chiborg Avatar asked Jan 23 '26 09:01

chiborg


1 Answers

I found this answer which also does what I want:

def action
    yield
    rescue
        ....
    ensure
        ....
end

action { call1 }
action { call2 }
action { call3 }
like image 151
chiborg Avatar answered Jan 26 '26 00:01

chiborg