Xanda's Blog !~!

Notes to Myself : Ruby Curb Curl::Err::HostResolutionError and Exception Handling

Sep
02

The following error occur when executing the following code WITH NO internet connection

#!/usr/bin/ruby
require 'rubygems'
require 'curb' #yerp you need to sudo gem install curb
 
def browse(url)
  c = Curl::Easy.new(url)
  c.connect_timeout = 3
  c.perform
  return c.body_str
end
 
url = gets
puts browse(url)

So to handle the error in ruby, I’ll next time use “begin” and “rescue”

#!/usr/bin/ruby
require 'rubygems'
require 'curb' #yerp you need to sudo gem install curb
 
def browse(url)
  c = Curl::Easy.new(url)
  begin
     c.connect_timeout = 3
     c.perform
     return c.body_str
  rescue
     return "Error in connection"
    end
end
 
url = gets
puts browse(url)

Resulting:


Just a notes to myself and at the same time “snip” code for others