From: Dave Burt Date: 2006-06-03T18:14:27+09:00 Subject: Re: Exceptions inside loops... darren kirby wrote: > begin telnetPorts.each do |port| > puts "Port " + port.to_s + ":" > t = TCPSocket.new(host,port) > banner = t.gets > t.close > end > > rescue SystemCallError > puts "Unable to connect to port: " + $! > end I see the question has been answered; let me just explain a little further. Here's the code you posted above with standard indentation: begin telnetPorts.each do |port| puts "Port " + port.to_s + ":" t = TCPSocket.new(host,port) banner = t.gets t.close end rescue SystemCallError puts "Unable to connect to port: " + $! end The first "end" closes the do..end block given to the each method. The second end closes the begin..end block. The rescue clause applies to the begin..end block. (Obviously) begin..end is used to introduce a new scope, often for exception handling like you're doing here, but you can see that if an exception is caught, you end up outside the loop. If you added a "retry" or "redo" to the rescue clause, you'd simply start the loop again from the top. You can add rescue clauses inside begin..end blocks and also def..end method bodies: def barf raise "some exception" rescue RuntimeError puts "some exception was raised" end Now you can go back to your already-working solution from Mike Fletcher's post. Cheers, Dave