Forum: Ruby Question about begin, rescue, end.

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.
7f891fbe8e3bae7f9fe375407ce90d9d?d=identicon&s=25 hhausman (Guest)
on 2005-11-18 03:49
(Received via mailing list)
Here's my code. It's dumb:

require 'open-uri'
begin
	while true do
		the_begin_time = Time.now
		open( "http://danceliquid.com/test/public/" ) do |stuff|
			puts stuff.read
		end
		the_wait_time = Time.now - the_begin_time
		the_file = File.new("SitePingLog.txt", "a")
		the_file.puts('Got it in '+ the_wait_time.to_s + ' seconds @ ' +
Time.now.to_s)
		the_file.close
		sleep 600
	end
rescue
		the_file = File.new("SitePingLog.txt", "a")
		the_file.puts(Error @ ' + Time.now.to_s)
		the_file.close
		retry
end

#What it does it hit my site every ten minutes, and keep a log of how
long it took to get the content there. I was having people tell me
that intermittently they wouldn't be able to get to it at all, or
sometimes it would be really slow. This script has proven this to be
true, and in all honesty I'm not too worried about the performance of
my site. My question is, all the open-uri code is inside the begin,
rescue section, and occasionally the rescue, end section gets called,
but also sometimes (infrequently), the whole script bombs out...
here's the error:

c:/ruby/lib/ruby/1.8/timeout.rb:42:in `rbuf_fill': execution expired
(Timeout::E
rror)
        from c:/ruby/lib/ruby/1.8/net/protocol.rb:196:in `timeout'
        from c:/ruby/lib/ruby/1.8/timeout.rb:55:in `timeout'
        from c:/ruby/lib/ruby/1.8/net/protocol.rb:196:in `rbuf_fill'
        from c:/ruby/lib/ruby/1.8/net/protocol.rb:160:in `readuntil'
        from c:/ruby/lib/ruby/1.8/net/protocol.rb:171:in `readline'
        from c:/ruby/lib/ruby/1.8/net/http.rb:1554:in `read_status_line'
        from c:/ruby/lib/ruby/1.8/net/http.rb:1538:in `read_new'
        from c:/ruby/lib/ruby/1.8/net/http.rb:833:in `request'
         ... 9 levels...
        from c:/ruby/lib/ruby/1.8/open-uri.rb:134:in `open_uri'
        from c:/ruby/lib/ruby/1.8/open-uri.rb:424:in `open'
        from c:/ruby/lib/ruby/1.8/open-uri.rb:85:in `open'
        from C:/Documents and
Settings/Harold/Desktop/scripting/ruby/SitePinger.
rb:5

... Right, so, isin't begin, rescue, end supposed to stop exactly this
from happening? Or chances are, I've misunderstood.

Thanks in advance for your time,
-Harold
31e038e4e9330f6c75ccfd1fca8010ee?d=identicon&s=25 gregory.t.brown (Guest)
on 2005-11-18 03:55
(Received via mailing list)
On 11/17/05, Harold Hausman <hhausman@gmail.com> wrote:

> ... Right, so, isin't begin, rescue, end supposed to stop exactly this
> from happening? Or chances are, I've misunderstood.
>
> Thanks in advance for your time,

I think it's the 'retry' that's causing the error.


This is being called inside of the rescue, which in turn has no rescue.

maybe call exit; at the end of your successful case, and then move
retry outside of the rescue, effectively creating a loop.
F0223b1193ecc3a935ce41a1edd72e42?d=identicon&s=25 zdennis (Guest)
on 2005-11-18 03:58
(Received via mailing list)
Harold Hausman wrote:

>
> .... Right, so, isin't begin, rescue, end supposed to stop exactly this
> from happening? Or chances are, I've misunderstood.
>

'rescue' by itself catches StandardError exceptions and subclasses of
StandardError. The
Timeout::Error is probably not a StandardError, but probably a subclass
of Exception. You can will
want to specfically catch Timeout::Error or Exception ....

begin
  ...
rescue Timeout::Error => ex # or rescue Exception => ex
  ...
end

HTH,

Zach
31e038e4e9330f6c75ccfd1fca8010ee?d=identicon&s=25 gregory.t.brown (Guest)
on 2005-11-18 04:14
(Received via mailing list)
On 11/17/05, zdennis <zdennis@mktec.com> wrote:
> Harold Hausman wrote:
>
> >
> > .... Right, so, isin't begin, rescue, end supposed to stop exactly this
> > from happening? Or chances are, I've misunderstood.
> >
>
> 'rescue' by itself catches StandardError exceptions and subclasses of StandardError. The
> Timeout::Error is probably not a StandardError, but probably a subclass of Exception.

Yup, that's what it is:
irb(main):004:0> Timeout::Error.ancestors
=> [Timeout::Error, Interrupt, SignalException, Exception, Object,
Kernel]

Ignore my first post and catch one of the above explicitly, and that
should solve your problem
(excluding Object and Kernel, of course ;) )
7f891fbe8e3bae7f9fe375407ce90d9d?d=identicon&s=25 hhausman (Guest)
on 2005-11-18 05:44
(Received via mailing list)
Very interesting.

I'd have guessed no arguments would catch anything, also strange how 2
functions in the same library (open-uri) would have different
exeception types. 500 errors return something that happens to be
caught as a StandardError while this Timeout::Error is of a whole
different lineage.  Oh well, I'm going with it. ;) We have to operate,
at least currently, under the assumption that the person who wrote the
library is hell of smarter than I am.

I trust that this will fix the problem, though I wont really ever know
for sure because the script only crashed out in that manner every few
days anyway.

Thanks again for all you guys' help. Long live Ruby.

-Harold
p.s. Are my initial questions getting double posted? or is gmail just
getting confused? 1000 apologies if they are getting double posted.
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 logancapaldo (Guest)
on 2005-11-18 06:02
(Received via mailing list)
On Nov 17, 2005, at 11:42 PM, Harold Hausman wrote:

> Very interesting.
>
> I'd have guessed no arguments would catch anything,

If you really want to catch _everything_:

begin
   ...
rescue Object => ex
  ...
end

Of course thats a little extreme.
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 vjoel (Guest)
on 2005-11-18 06:56
(Received via mailing list)
Logan Capaldo wrote:

> If you really want to catch _everything_:
>
> begin
>   ...
> rescue Object => ex

  rescue Exception => ex
31e038e4e9330f6c75ccfd1fca8010ee?d=identicon&s=25 gregory.t.brown (Guest)
on 2005-11-18 07:05
(Received via mailing list)
>
> -Harold
> p.s. Are my initial questions getting double posted? or is gmail just
> getting confused? 1000 apologies if they are getting double posted.
>

No. that's just gmail.  Annoying, isn't it?
5befe95e6648daec3dd5728cd36602d0?d=identicon&s=25 bob.news (Guest)
on 2005-11-18 10:12
(Received via mailing list)
A general remark: You can greatly improve reliability of your code by
replacing

the_file = File.new("SitePingLog.txt", "a")
the_file.puts('Got it in '+ the_wait_time.to_s + ' seconds @ ' +
Time.now.to_s)
the_file.close

with

File.open("SitePingLog.txt", "a") do |the_file|
  the_file.puts('Got it in '+ the_wait_time.to_s + ' seconds @ ' +
    Time.now.to_s)
end

Kind regards

    robert
E555e7c34196967444a47a96395a23ab?d=identicon&s=25 skaes (Guest)
on 2005-11-18 15:48
(Received via mailing list)
Robert Klemme wrote:

>File.open("SitePingLog.txt", "a") do |the_file|
>  the_file.puts('Got it in '+ the_wait_time.to_s + ' seconds @ ' +
>    Time.now.to_s)
>end
>
>
I'd prefer

File.open("SitePingLog.txt", "a") do |file|
  file.puts("Got it in #{the_wait_time} seconds @ #{Time.now}")
end

;-)
912c61d9da47754de7039f4271334a9f?d=identicon&s=25 mental (Guest)
on 2005-11-18 17:16
(Received via mailing list)
Quoting Logan Capaldo <logancapaldo@gmail.com>:

>    ...
> rescue Object => ex
>   ...
> end
>
> Of course thats a little extreme.

Can you _raise_ a non-Exception-derived class?  I don't think you
can...

-mental
This topic is locked and can not be replied to.