From: Guillaume Marcais Date: 2005-04-04T23:28:06+09:00 Subject: Re: Handling Timeout::Error from TCPSocket On Sat, 2005-04-02 at 20:05 +0900, Pat Maddox wrote: > def execute > status = timeout(@timeoutval) { > socket = TCPSocket.new(@host, @port) rescue false > > socket.close if socket > return socket != false > } rescue Timeout::Error ^^^^^^^^^^^^^^^^^^^^^ This probably doesn't do what you expect. The argument to rescue for the inline version is the value to return in case an exception is raised and caught. Which is different from the block version where it is an exception class. The block version with no argument or the inline version filters all exception that inherit from StandardError. But Timeout::Error does not inherit from StandardError. So it punches right through. The code you wrote returns the class Timeout::Error if a StandardError is raised. Example: $ irb irb(main):001:0> require 'timeout' => true irb(main):002:0> 1 rescue 0 => 1 irb(main):003:0> raise "bad" rescue 0 => 0 irb(main):004:0> raise "Bad" rescue Timeout::Error => Timeout::Error irb(main):005:0> raise Timeout::Error, "Catch me if you can" rescue 0 (irb):5:in `irb_binding': Catch me if you can (Timeout::Error) from /usr/lib/ruby/1.8/irb/workspace.rb:52:in `irb_binding' from /usr/lib/ruby/1.8/irb/workspace.rb:52 If you look at Sam's code snippet, he correctly uses begin/rescue/end to catch the Timeout::Error exception. A final note on the inline rescue: it has a higher precedence than =, so you would need parenthesis to assign the rescue value: $ irb irb(main):001:0> a = 0 => 0 irb(main):002:0> a = raise "bad" rescue 1 => 1 irb(main):003:0> a => 0 irb(main):004:0> a = (raise "bad" rescue 1) => 1 irb(main):005:0> a => 1 Hope it helps, Guillaume.