0
begin
  ie.select_list(:id, "PageSize").select_value("300")
rescue Watir::Exception::UnknownObjectException  
  aftererrorObj.errorMain(ie,con,country)
  retry
rescue Timeout::Error
  aftererrorObj.errorMain(ie,con,country)
  retry 
end

my ruby code is handling exception in this way.Is this right way to handle exception? will it work?

Justin Ko
  • 46,526
  • 5
  • 91
  • 101
unknownbits
  • 2,855
  • 10
  • 41
  • 81
  • 3
    Why are you retrying in the event of an exception? Will something change that will change the action from failing to working? For example, if the element is loaded via ajax, rescuing/retrying the timeout might work. However, you should really design the script to expect it by using one of the wait methods. – Justin Ko Jul 08 '13 at 13:01
  • but I want to know, can I rescue these exception using one rescue ? – unknownbits Aug 03 '13 at 10:16

2 Answers2

2

If you're just looking to rescue the script in case the object has not loaded yet due to AJAX or slow response time, something like this might be more appropriate:

Watir::Wait.until(60) { ie.select_list(:id, "PageSize").exists? }

OR

ie.select_list(:id, "PageSize").when_present.select_value("300)
adam reed
  • 2,024
  • 18
  • 24
0

If just want to catch multiple types of exceptions in the same rescue, combine them as comma separated list:

begin
  ie.select_list(:id, "PageSize").select_value("300")
rescue Watir::Exception::UnknownObjectException, Timeout::Error
  aftererrorObj.errorMain(ie,con,country)
  retry
end
Justin Ko
  • 46,526
  • 5
  • 91
  • 101
  • can I do it like that "rescue Exception"as it is super class of all exceptions – unknownbits Aug 03 '13 at 16:10
  • 2
    Yes, you can. However, it is not recommended - see this [other question for an explanation](http://stackoverflow.com/questions/10048173/why-is-it-bad-style-to-rescue-exception-e-in-ruby). – Justin Ko Aug 03 '13 at 17:11