0

The problem is in relation to the radis-rb gem.

The exception is not caught by my rescue block and my app goes down.

My code:

begin
  redis = Redis.new 
  puts "WTF?"
rescue Exception
  puts "Exception"
end

If redis is down, the message WTF? is exposed.

It happens with or without the Exception statement.

Why is the exception not raised to my rescue block?

Paul Brit
  • 5,833
  • 4
  • 20
  • 22
  • Please [don't ever rescue from Exception](http://stackoverflow.com/questions/10048173/why-is-it-bad-style-to-rescue-exception-e-in-ruby)! – Andrew Marshall Apr 27 '12 at 16:49
  • @Andrew: While that's true, it's not really helpful in this case because `Errno::ECONNREFUSED` should inherit from `Exception` (at least if the Gem doesn't replace it...). As I read it from the answer, OP has already tried a plain `rescue`: "It is still so with/without Exception statement." – Niklas B. Apr 27 '12 at 16:50
  • @NiklasB. I know, but it's still not a good idea to rescue from it in the first place (hence this being a comment). – Andrew Marshall Apr 27 '12 at 16:51
  • 3
    On a more related note, is that call to `Redis.new` actually in the stacktrace from the exception? – Andrew Marshall Apr 27 '12 at 16:54
  • Just rescue from Errno::ECONNREFUSED – Ismael Apr 27 '12 at 17:18
  • `WTF?` printed. I update my question. Rescue from Errno::ECONNREFUSED - result is the same – Paul Brit Apr 27 '12 at 17:33
  • 2
    So what is the backtrace ? Sounds like the call to Redis.new isn't what is throwing the exception – Frederick Cheung Apr 27 '12 at 17:47
  • Thank you for conversation! Exception is raised (in IRB) when IRB try to print the result of `Redis.new` call. Exception isn't raised (in script) because `redis ruby gem` not raise exception if redis service is down. – Paul Brit Apr 27 '12 at 18:05
  • @PaulChechetin Until you provide the backtrace from the exception and verify that your code provided is a part of the backtrace, no one can answer your question. – Andrew Marshall Apr 27 '12 at 18:19
  • The problem is has been solved, thank you! – Paul Brit Apr 27 '12 at 18:41
  • @PaulChechetin perhaps you should extract your answer from the question and answer the question yourself, which is quite reasonable – Jonathan Apr 27 '12 at 19:12
  • @defaye I can't do that because my reputation is too few. I will do that later ( after about 8 hour ) – Paul Brit Apr 27 '12 at 20:13
  • My bad: thought you had 8 hours :) – Jonathan Apr 27 '12 at 21:00
  • @defaye I'm so sorry for my broken English. – Paul Brit Apr 27 '12 at 21:51
  • @PaulChechetin it's there to help others and you. Nothing personal or bad. :) – Jonathan Apr 27 '12 at 23:05

1 Answers1

1

I have solved the problem myself (with help from the community and comments).

The exception occurred in IRB only.

The reason of one is IRB's inspect call when IRB try to print result of Redis.new.

In the script (not IRB), the exception doesn't occur because Redis.new does not raise an exception if the Redis service is down.

This question helped to solve my problem.

Community
  • 1
  • 1
Paul Brit
  • 5,833
  • 4
  • 20
  • 22