Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Recovering from an exception from within a loop

Status
Not open for further replies.

edpatterson

IS-IT--Management
Feb 24, 2005
186
This was not copied from the IDE but entered directly into the form. It should be close enough to get the point across.


:source.txt:
machine1
machine2
machine3
machine4

machine3 is not in DNS
Code:
dim sr as StreamReader = New StreamReader("c:\source.txt")
dim strComputer as Sting = sting.empty
try
	while not sr.AtEndOfStream
		strComputer = sr.readline
		if my.computer.network.ping(strComputer) Then
			lbAlive.items.add(strComputer)
		End if
	End While
catch ex as exception
	msgbox(ex.message & " " & strComputer,,"Error...")
End Try

When the loop hits machine3 an exception is thrown and the loop exits. I would like it to resume with machine4.

I actually have 1500 machines to check so production code it radically different :)

 
Try moving the loop outside of the try block...

Code:
dim sr as StreamReader = New StreamReader("c:\source.txt")
dim strComputer as Sting = sting.empty
while not sr.AtEndOfStream
    try
        strComputer = sr.readline
        if my.computer.network.ping(strComputer) Then
            lbAlive.items.add(strComputer)
        End if
    catch ex as exception
        msgbox(ex.message & " " & strComputer,,"Error...")
    End Try
End While

You also might want to put all the exceptions into an array or variable of some sort and just show that as a message at the end or log it or whatever. Otherwise, every time the Ping creates the exception your code will stop until you close the message box.

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB/Access Programmer
 
T H W A C K !

Sound palm smacking forehead. It is a darned good thing I do not program from a living. I would not be near as fat.

Thanks!
Ed
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top