Problem with exception not beeing caught

  • Thread starter Michael Mueller
  • Start date
M

Michael Mueller

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
 
M

Mark Hubbart

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?

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
 
M

Michael Mueller

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 :)

Thanks!

Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top