Jan 16, 2011

Rescue from 'execution expired' in Ruby

I recently wrote some scripts which were unexpectedly exiting with an ‘execution expired’ message.  A normal catch all exception doesn’t work, because Timeout::Error apparently derives from Interruption, and not StandardError (thanks lindsaar.net). So, I had to rescue it explicitly like so:


begin
  # do something involving Net:HTTP
rescue => e
  # This will NOT catch 'execution expired' !!
  puts "Rescue : #{e}"
rescue Timeout::Error => te
  # This explicit rescue will work
  puts "Rescued from timeout : #{te}"
end

It was driving me a little batty for a while, so I thought I’d share it.