Problem with exception not beeing caught

Dear rubyists!

I have a problem with a small programm, that's fetching emails from my
account and then goes to sleep for X seconds.
The code looks somehow like this:

loop do
  begin
    load FILTERFILE
    BOXES.each{|box| check_mailbox(box)}
    $log.debug("Gehe für #{SLEEPTIME} Sekunden schlafen!")
    sleep(SLEEPTIME)
  rescue
    $log.warn("Fehler aufgetreten:#{$!}\n#{$!.backtrace}")
  end
end

From time to time I get an exception in check_mailbox because the mailserver
doesn't respond so I'm gettin:
mi@ceres:~$ mail-forward.rb -n
/usr/local/lib/ruby/1.8/timeout.rb:42:in `new': execution expired
(Timeout::Error)
        from /usr/local/lib/ruby/1.8/net/protocol.rb:83:in `connect'
        from /usr/local/lib/ruby/1.8/net/protocol.rb:82:in `timeout'
        from /usr/local/lib/ruby/1.8/timeout.rb:55:in `timeout'
        from /usr/local/lib/ruby/1.8/net/protocol.rb:82:in `connect'
        from /usr/local/lib/ruby/1.8/net/protocol.rb:64:in `initialize'
        from /usr/local/lib/ruby/1.8/net/pop.rb:427:in `open'
        from /usr/local/lib/ruby/1.8/net/pop.rb:427:in `do_start'
        from /usr/local/lib/ruby/1.8/net/pop.rb:415:in `start'
        from /home/mi/bin/mail-forward.rb:45:in `check_mailbox'
        from /home/mi/bin/mail-forward.rb:89
        from /home/mi/bin/mail-forward.rb:89:in `each'
        from /home/mi/bin/mail-forward.rb:89
        from /home/mi/bin/mail-forward.rb:86:in `loop'
        from /home/mi/bin/mail-forward.rb:86

I guess that's kinda normal, but: Why isn't this exception caught??? I
expected the exception in my logfile and the program to continue, but
obviously the program exits before getting into the rescue block??

Any ideas what i'm doing wrong?

Michael

When you don't specify an exception class to rescue from, it defaults to StandardError. Timeout::Error doesn't inherit from StandardError, so it goes uncaught:

   # show the ancestors: no StandardError!
   Timeout::Error.ancestors
     ==>[Timeout::Error, Interrupt, SignalException, Exception, Object, Kernel]

   # test rescuing the lowest exception class possible: Exception.
   begin
     raise Timeout::Error
   rescue Exception
     p 'rescued!'
   end
   "rescued!"
     ==>nil

   # test an unspecified exception class:
   begin
     raise Timeout::Error
   rescue
     p 'rescued!'
   end
   (irb):9:in `irb_binding': Timeout::Error (Timeout::Error)
         from /usr/local/lib/ruby/1.9/irb/workspace.rb:52:in `irb_binding'
         from /usr/local/lib/ruby/1.9/irb/workspace.rb:52

In fact, this made IRB die off on me; IRB didn't catch the error either.

HTH,
Mark

···

On Aug 9, 2004, at 12:16 AM, Michael Mueller wrote:

Dear rubyists!

I have a problem with a small programm, that's fetching emails from my
account and then goes to sleep for X seconds.
The code looks somehow like this:

loop do
  begin
    load FILTERFILE
    BOXES.each{|box| check_mailbox(box)}
    $log.debug("Gehe für #{SLEEPTIME} Sekunden schlafen!")
    sleep(SLEEPTIME)
  rescue
    $log.warn("Fehler aufgetreten:#{$!}\n#{$!.backtrace}")
  end
end

From time to time I get an exception in check_mailbox because the mailserver
doesn't respond so I'm gettin:
mi@ceres:~$ mail-forward.rb -n
/usr/local/lib/ruby/1.8/timeout.rb:42:in `new': execution expired
(Timeout::Error)
        from /usr/local/lib/ruby/1.8/net/protocol.rb:83:in `connect'
        from /usr/local/lib/ruby/1.8/net/protocol.rb:82:in `timeout'
        from /usr/local/lib/ruby/1.8/timeout.rb:55:in `timeout'
        from /usr/local/lib/ruby/1.8/net/protocol.rb:82:in `connect'
        from /usr/local/lib/ruby/1.8/net/protocol.rb:64:in `initialize'
        from /usr/local/lib/ruby/1.8/net/pop.rb:427:in `open'
        from /usr/local/lib/ruby/1.8/net/pop.rb:427:in `do_start'
        from /usr/local/lib/ruby/1.8/net/pop.rb:415:in `start'
        from /home/mi/bin/mail-forward.rb:45:in `check_mailbox'
        from /home/mi/bin/mail-forward.rb:89
        from /home/mi/bin/mail-forward.rb:89:in `each'
        from /home/mi/bin/mail-forward.rb:89
        from /home/mi/bin/mail-forward.rb:86:in `loop'
        from /home/mi/bin/mail-forward.rb:86

I guess that's kinda normal, but: Why isn't this exception caught??? I
expected the exception in my logfile and the program to continue, but
obviously the program exits before getting into the rescue block??

Any ideas what i'm doing wrong?

Hi Mark,

When you don't specify an exception class to rescue from, it defaults
to StandardError. Timeout::Error doesn't inherit from StandardError, so
it goes uncaught:

Ups! Didn't think about that possibility!
I changed the code the rescue Timeout::Error and now it works :slight_smile:

Thanks!

Michael