Forum: Ruby How to catch Timeout::ERROR ?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6f4f8c4c221ef48cc3291db1a2be2cf2?d=identicon&s=25 Verestiuc Vlad (Guest)
on 2005-12-13 03:02
(Received via mailing list)
Is there any way to catch Timeout::ERROR exception throw by timeout
() ?

====[cut here]======
#!/usr/bin/env ruby


require 'open-uri'
require 'timeout'


begin
         open("http://6.6.6.6/"){}
rescue => e
         puts e.message
         puts "I'm still running"
end
====[and here]====

output
/sw/lib/ruby/1.8/timeout.rb:42:in `new': execution expired
(Timeout::Error)
         from /sw/lib/ruby/1.8/net/protocol.rb:83:in `connect'
         from /sw/lib/ruby/1.8/net/protocol.rb:82:in `timeout'
         from /sw/lib/ruby/1.8/timeout.rb:55:in `timeout'
         from /sw/lib/ruby/1.8/net/protocol.rb:82:in `connect'
         from /sw/lib/ruby/1.8/net/protocol.rb:64:in `initialize'
         from /sw/lib/ruby/1.8/net/http.rb:430:in `open'
         from /sw/lib/ruby/1.8/net/http.rb:430:in `do_start'
         from /sw/lib/ruby/1.8/net/http.rb:419:in `start'
          ... 6 levels...
         from /sw/lib/ruby/1.8/open-uri.rb:134:in `open_uri'
         from /sw/lib/ruby/1.8/open-uri.rb:422:in `open'
         from /sw/lib/ruby/1.8/open-uri.rb:85:in `open'
         from /Users/eck/test.rb:9
----------------------------------------
ruby -v
ruby 1.8.2 (2004-12-25) [powerpc-darwin8.0]




---
Verestiuc Vlad
Net 4 All Grup S.R.L.
Phone: +40724567695
http://www.n4a.ro/
------------------------------------------------------------------------
---
Privileged/Confidential Information may be contained in this message. If
you are not the addressee indicated in this message (or responsible for
delivery of the message to such a person), you may not copy or
deliver this
message to anyone. In such a case, you should destroy this message and
kindly notify the sender by reply e-mail.
------------------------------------------------------------------------
---
58479f76374a3ba3c69b9804163f39f4?d=identicon&s=25 Eric Hodel (Guest)
on 2005-12-13 05:39
(Received via mailing list)
On Dec 12, 2005, at 6:00 PM, Verestiuc Vlad wrote:

> Is there any way to catch Timeout::ERROR exception throw by timeout
> () ?

Timeout::Error

> rescue => e
rescue Timeout::Error => e

>         puts e.message
>         puts "I'm still running"
> end

Beware!

You should use a subclass of Timeout::Error when nesting timeouts
(Net::HTTP has timeouts inside) so that you don't trap the wrong
timeout in the wrong place and run forever:

irb(main):002:0> open('http://localhost:8080/')
/usr/local/lib/ruby/1.8/timeout.rb:43:in `rbuf_fill': execution
expired (Timeout::Error)

--
Eric Hodel - drbrain@segment7.net - http://segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com
This topic is locked and can not be replied to.