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

  1. Bryn Thomas Says:

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

  2. Roob Says:

    that was very helpful, thanks!

  3. dace Says:

    this was a huge help!

  4. Robert Aganauskas Says:

    Thanks, this error was really driving me crazy.

  5. Jarek Says:

    Thanks !

  6. Jan Rüegg Says:

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

  7. Murali Says:

    Thanks. this saved my debugging time.

  8. Dirceu Jr. Says:

    Thx!!!

  9. Tudor Constantin Says:

    This was a great help. Small, concise, exactly what I needed. Thx alot

  10. Jon Says:

    Thanks! Found this on first google search, instantly fixed!

  11. Dan D Says:

    Thank you very much. But…. so lame…

  12. jay11 Says:

    You know your projects stand out of the herd. There is something special about them. It seems to me all of them are really brilliant!
    brisbane bathroom renovations

  13. jay11 Says:

    Your blog provided us with valuable information to work with. Each & every tips of your post are awesome. Thanks a lot for sharing. Keep blogging,
    http://www.roulette-fr.net/

  14. jay11 Says:

    Hello, I have browsed most of your posts. This post is probably where I got the most useful information for my research. Thanks for posting, maybe we can see more on this. Are you aware of any other websites on this subject.
    purchase hgh

  15. John Says:

    You should not have a catch-all rescue anyway.

    “rescue => e” == EVIL.

  16. dubek Says:

    @Colin

    You don’t want to rescue Exception, because then even the syntax errors in your code will be rescued by the rescue clause.

    I still don’t understand why the Timeout exceptions aren’t a subclass of StandardError…

  17. Colin Curtin Says:

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

    Every exception derives from Exception, so you can do

    begin

    1. Thing that throws Timeouts, anything…
      rescue Exception => e
      puts “#{e}\n#{e.backtrace.join(”\n")}"
      end
  18. Chris Says:

    Very helpful – accurate and to the point. Thank you!

  19. hyghvgf Says:

    Thanks! Found this on first google search, instantly fixed!
    emengpsfon jhf nsvbqxdb dsj nomtchiche ft oreergonst

  20. mic1122 Says:

    Its a great pleasure reading your post.Its full of information I am looking for and I love to post a comment that “The content of your post is awesome” Great work.
    casino-legal.info

  21. asdsdsd Says:

    Hello There. I found your blog using msn. This is an extremely well written article. I will be sure to bookmark it and return to read more of your useful information. Thanks for the post. I’ll certainly comeback.
    World Housing & Relationships

  22. prince Says:

    I am very enjoyed for this blog. Its an informative topic. It help me very much to solve some problems. Its opportunity are so fantastic and working style so speedy.
    best funny website

  23. prince Says:

    I am very enjoyed for this blog. Its an informative topic. It help me very much to solve some problems. Its opportunity are so fantastic and working style so speedy.
    best funny website

  24. prince Says:

    Great article with excellent idea!Thank you for such a valuable article. I really appreciate for this great information..
    Relationship Issues by PB

  25. tareelolo Says:

    Mostly my oversight, but also if Fieri was hosting on the set of his (hilarious) Food Network cooking show, he’d at least have a big television. And plenty of dippin-aise. And he’d be shouting and there’d be like a Brian Setzer Orchestra album on really loud. So in retrospect I shouldn’t have left it out….. usb safe

  26. azee1122 Says:

    This is a great inspiring article.I am pretty much pleased with your good work.You put really very helpful information…
    www.blackzonestudio.com

  27. differentsunvids.com Says:

    I’m not sure why but this weblog is loading very slow for me. Is anyone else having this problem or is it a issue on my end? I’ll check back later and see if the
    problem still exists.

  28. tareelolo Says:

    The drop-shipping program has very advantageous prices, similar to wholesale. You do not need to invest any money to stock products!
    hardware keylogger

  29. KarlCrisson Says:

    I envy because I never got a chance to have to. However, I am still happy that the parents are spending much of their time for the kids.PS3 Emulator

  30. MrEmo Says:

    I have explain so many article of this site in which some of them were very intresting and inspiring.This article has good title with good description.Xbox Emulator

  31. casino gaming Says:

    Hi there, I do think your web site could be having
    internet browser compatibility problems. When I look at
    your website in Safari, it looks fine however, if opening in IE, it
    has some overlapping issues. I merely wanted to provide you with a quick heads up!
    Besides that, fantastic site!

  32. new casino bankruptcy Says:

    hey there and thank you for your info – I’ve certainly picked up anything new from right here. I did however expertise a few technical points using this website, since I experienced to reload the site a lot of times previous to I could get it to load properly. I had been wondering if your web host is OK? Not that I am complaining, but slow loading instances times will sometimes affect your placement in google and could damage your high quality score if advertising and marketing with Adwords. Anyway I am adding this RSS to my email and can look out for a lot more of your respective intriguing content. Make sure you update this again very soon.

  33. lemiPieftof Says:

    Children’s tooth development begins while the baby is in the womb. Teething usually occurs between the ages of six and nine months. Children usually have their full set of 20 primary teeth (milk teeth, baby teeth or deciduous teeth) by the age of three years. At about the age of six years, the first permanent teeth erupt (push through the gum).

  34. www Says:

    This is very interesting, You’re a very skilled blogger. I have joined your feed and look forward to seeking more of your fantastic post. Also, I have shared your site in my social networks! www http://sportsdiviner.net/kosa/

  35. pistolet hukowy Says:

    This will be a fantastic web site, might you be interested in doing an interview regarding just how you designed it? If so e-mail me! pistolet hukowy http://www.porcja.eu/sztucereu-wykrywacze-metalu

  36. strona firmowa Says:

    Reading this write-up – the present of your time strona firmowa http://mjk-serwis.pl/2013/02/12/

  37. Victor1122 Says:

    It is a great website.. The Design looks very good.. Keep working like that!.
    electronic cigarette reviews

  38. Posadzki białystok Says:

    Great beat ! I would like to apprentice while you amend your site, how could i subscribe for a blog site? The account aided me a acceptable deal. I had been a little bit acquainted of this your broadcast offered bright clear idea Posadzki białystok http://przedszkolezielonagora.com.pl/przydomowe-oczyszczalnie-sciekow-bialystok/

  39. chris1258 Says:

    Great post. bean bags It’s good to see you to verbalize your heart and your clarity on this important issue can be easily detected. chairs Looking forward to read more. furniture furniture|sofa|beanbagnerd.com sofa

  40. www Says:

    I definitely wanted to construct a brief note in order to appreciate you for these lovely guides you are placing on this website. My time intensive internet lookup has at the end of the day been rewarded with sensible information to exchange with my guests. I would point out that we website visitors are very lucky to be in a useful network with very many awesome professionals with very beneficial plans. I feel very blessed to have come across your entire website and look forward to really more cool minutes reading here. Thanks a lot again for everything. www http://www.cyllene.eu/stadnina-kopytko-hotel-dla-koni-pomorskie

  41. chartering a private plane Says:

    Does your blog have a contact page? I’m having trouble locating it but, I’d like to shoot you an email. I’ve got some creative ideas for your blog you might be interested in hearing. Either way, great website and I look forward to seeing it grow over time.

Leave a Reply