This is a fun little exception in Ruby that you have to catch explicitly in order to get hold of it in a rescue block.

Say you had the following:

1
2
3
4
5
6
7
require 'net/pop3'
begin
  Net::POP3.auth_only(@server, @port, @username, @password)
rescue => e
  write_error_to_logfile(e)
  do_something_sensible
end

And the mail server could not be reached due to transient network problems -> that is, you are getting a socket timeout error.

Well, your code in the rescue block won’t get executed! Even though the script raises an Error!

Why is this?

Simple answer, because Timeout::Error is not a subclass of StandardError, it is a subclass of the Interrupt class.

So, that means you have to catch it explicitly, like so:

1
2
3
4
5
6
7
8
9
10
require 'net/pop3'
begin
  Net::POP3.auth_only(@server, @port, @username, @password)
rescue => e
  write_error_to_logfile(e)
  do_something_sensible
rescue Timeout::Error => e
  write_error_to_logfile(e)
  do_something_sensible_for_timeout
end

And all will be good again, error caught!

blogLater

Mikel

11 Responses to “'rbuf_fill': execution expired (Timeout::Error)”

  1. xuxu Says:

    Thank you very much ;)

  2. Bryn Thomas Says:

    Thanks, I had this same error and this cleared up my incorrect expectation that everything derived from a root Exception class.

  3. Colin Curtin Says:

    http://blog.nicksieger.com/articles/2006/09/06/rubys-exception-hierarchy

    Every exception derives from Exception, so you can do

    begin # Thing that throws Timeouts, anything… rescue Exception => e puts ”#{e}\n#{e.backtrace.join(”\n”)}” end

  4. dace Says:

    this was a huge help!

  5. Jan Rüegg Says:

    This was e great help to me too… Thanks a lot!

  6. Robert Aganauskas Says:

    Thanks, this error was really driving me crazy.

  7. Jarek Says:

    Thanks !

  8. Roob Says:

    that was very helpful, thanks!

  9. Murali Says:

    Thanks. this saved my debugging time.

  10. Dirceu Jr. Says:

    Thx!!!

  11. wc Says:

    thank you, this was very helpful

Leave a Reply