Handling exceptions

I’m using a web service in Ruby script and occasionally I get errors. I
want to add error handling code, but do not know how to describe the
error to Ruby. Here’s the error:

GeoNames::APIError: {“message”=>“ERROR: canceling statement due to
statement timeout”, “value”=>13}

What do I put after “rescue”? I can’t leave it blank because I have more
than one error to capture.

Service is geonames.org BTW.

Another error I have to capture is:

GeoNames::APIError: {“message”=>“we are afraid we could not find a
neighbourhood for latitude and longitude :33.793038,-118.327683”,


PS Preview isn’t working. I didn’t see any returns/paragraphs in the

If it’s the same error class you’d have to use the same rescue and then
test the error content after rescuing in order to deal with it

something like this

rescue GeoNames::APIError => err
case err.message
when /timeout/
# Handle ‘timeout’ error
when /could not find/
# Handle ‘could not find’ error
# Unhandled error
raise err

Thank you. I didn’t understand the syntax.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs