【发布时间】:2011-09-21 05:35:00
【问题描述】:
这是我的红宝石程序
require 'net/http'
require 'uri'
begin
url = URI.parse("http://google.com")
rescue Exception => err
p err
exit
end
http = Net::HTTP.new(url.host, url.port)
res = http.head("/")
p res.code
它工作正常,但是如果我从 URL.parse() 中删除 http://,它会给我这个错误:
/usr/lib/ruby/1.9.1/net/http.rb:1196:in `addr_port': undefined method `+' for nil:NilClass (NoMethodError) ...
from /usr/lib/ruby/1.9.1/net/http.rb:1094:in `request'
from /usr/lib/ruby/1.9.1/net/http.rb:860:in `head'
处理异常的方法正确吗?
我知道 URL 可能不正确,但它应该引发异常 URI::InvalidURIError 而不是接受并继续程序?
【问题讨论】:
标签: ruby exception net-library