From: darren kirby Date: 2006-06-04T01:35:40+09:00 Subject: Re: Exceptions inside loops... --nextPart1873791.A1dPdozLbS Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline quoth the Dave Burt: > darren kirby wrote: > > begin telnetPorts.each do |port| > > puts "Port " + port.to_s + ":" > > t =3D TCPSocket.new(host,port) > > banner =3D 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 furthe= r. > > Here's the code you posted above with standard indentation: > > begin > telnetPorts.each do |port| > puts "Port " + port.to_s + ":" > t =3D TCPSocket.new(host,port) > banner =3D 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 Thanks for the additional enlightenment Dave, I did see that syntax in some= =20 sample code but I was getting "Unexpected KRescue or somesuch error. Howeve= r,=20 now it is working as it should, and I just need to find out how to send=20 Socket a timeout (SO_RCVTIMEO seems not to be defined) but that is another= =20 post! Thanks guys, =2Dd =2D-=20 darren kirby :: Part of the problem since 1976 :: http://badcomputer.org "...the number of UNIX installations has grown to 10, with more expected..." =2D Dennis Ritchie and Ken Thompson, June 1972 --nextPart1873791.A1dPdozLbS Content-Type: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2.2 (GNU/Linux) iD8DBQBEgbpZwPD5Cr/3CJgRAhrcAKCzru2LODJV6HFFR5IugLyj+umiHQCgxSmZ YOAle4D8zpBIE7oJEQwwmjQ= =h1Ts -----END PGP SIGNATURE----- --nextPart1873791.A1dPdozLbS--