Mark Probert
2/9/2005 11:28:00 PM
Hi ..
On Wednesday 09 February 2005 11:31, Kent Sibilev wrote:
> Mark Probert <probertm@acm.org> writes:
> > Can someone please let me know what I am doing wrong here?
> > I can't seem to catch the exception ..
> >
>
> Exception is being thrown from the outside of begin...end construct. Try
> this:
>
Many thanks. The t.close() in the ensure is out of scope, so that the final
result looks like:
require 'socket'
def alive(host, port=80)
s = "host=#{host} : "
t = TCPSocket.new(host, port)
s << " made it!"
t.close
return s
rescue Errno::ETIMEDOUT
s << " Timed out -- node unreachable"
rescue Exception => e
s << " exception = #{e}"
end
puts alive('10.10.10.2')
So, the next question I have is how does this actually work? Is there some
kind of implicit block around the code? If so, why does the explicit
begin .. end construct not work? How can I use an ensure for this?
Also, whilst I am at it ;-), I used the first formulation
t = TCPSocket.new()
begin
<stuff>
rescue <errors>
ensure
t.close
end
because t is not in scope if the creator is inside the begin .. end block. I
recall that this is the subject of much debate. How has it been resolved?
--
-mark. (probertm at acm dot org)