Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > (Simple?) Why is my rescue clause not catching an exception?

Reply
Thread Tools

(Simple?) Why is my rescue clause not catching an exception?

 
 
Wes Gamble
Guest
Posts: n/a
 
      11-20-2006
All,

I'm using the Net::HTTP module to fetch resources from the Internet in a
Rails application.

I have a method that does the HTTP request and this action is enclosed
in a begin/rescue/end block. However, when I just did a request and the
Internet connectivity went down for a second, the call to
"http.request(req)" failed and I didn't see my rescue block get
executed. Instead, it appears that the original exception (from
timeout.rb) just bubbles all of the way up the stack and my rescue
doesn't seem to catch it.

Is this because the call to http.request is inside of it's own block and
I would need to place a rescue clause in there?

Here's the guts of my method:

begin
if (theURL.instance_of?(URI::HTTP))
req = Net::HTTP::Get.new(theURL.request_uri())
res = Net::HTTP.start(theURL.host, theURL.port) {|http|
http.request(req)
}
end
rescue
raise("Unable to get page - please check the URL")
end

Thanks,
Wes

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      11-20-2006
On 20.11.2006 18:34, Wes Gamble wrote:
> All,
>
> I'm using the Net::HTTP module to fetch resources from the Internet in a
> Rails application.
>
> I have a method that does the HTTP request and this action is enclosed
> in a begin/rescue/end block. However, when I just did a request and the
> Internet connectivity went down for a second, the call to
> "http.request(req)" failed and I didn't see my rescue block get
> executed. Instead, it appears that the original exception (from
> timeout.rb) just bubbles all of the way up the stack and my rescue
> doesn't seem to catch it.
>
> Is this because the call to http.request is inside of it's own block and
> I would need to place a rescue clause in there?
>
> Here's the guts of my method:
>
> begin
> if (theURL.instance_of?(URI::HTTP))
> req = Net::HTTP::Get.new(theURL.request_uri())
> res = Net::HTTP.start(theURL.host, theURL.port) {|http|
> http.request(req)
> }
> end
> rescue
> raise("Unable to get page - please check the URL")
> end
>
> Thanks,
> Wes
>


rescue catches StandardError by default only and the exception thrown by
Net::HTTP is likely some other type not inheriting standard error.

>> StandardError.ancestors

=> [StandardError, Exception, Object, Kernel]

You need

rescue Exception

Kind regards

robert
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
rescue clause Patrick Doyle Ruby 3 10-10-2008 03:42 PM
BIG ERROR:class or module required for rescue clause (TypeEr W0rtex Indigo Ruby 3 06-07-2007 08:18 PM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
except clause not catching IndexError Derek Schuff Python 7 02-23-2006 12:42 PM



Advertisments