By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,534 Members | 943 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,534 IT Pros & Developers. It's quick & easy.

exiting a for loop.

P: n/a
I have a for loops that goes from 1 to 256
I test the number and exception is thrown if there is an error.

How do I get the program to stop in the middle of the loop and go to the
next increment if an exception is thrown. I can only figure out how to exit
the loop.

'read the text file

line = sr.ReadLine()

Do While Not line Is Nothing

subnet = line.Remove(line.Length - 1, 1)

For ip = 1 To 256

computer = subnet & ip

' Connect to IP and send results

Dim scope As New ManagementScope("\\" & computer & "\root\cimv2")

Dim objectQuery As New ObjectQuery("select * from Win32_OperatingSystem")

Dim searcher As New ManagementObjectSearcher(scope, objectQuery)

Dim os As ManagementObject

Try

scope.Connect()

Catch ex As Exception

WOULD LIKE TO EXIT LOOP HERE AND GO ON TO NEXT NUMBER

End Try

For Each os In searcher.Get()

osname = os("caption")

dirname = os("windowsdirectory")

servername = os("name")

End If

Next os

Nov 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
To do it clean

Set the try inside your for next
And when needed created 2 seperated trys

(A try block can exist inside a try block)

I hope this helps?

Cor
Nov 21 '05 #2

P: n/a
On 2004-08-29, Microsoft <ma*@mas.com> wrote:
I have a for loops that goes from 1 to 256
I test the number and exception is thrown if there is an error.

How do I get the program to stop in the middle of the loop and go to the
next increment if an exception is thrown. I can only figure out how to exit
the loop.


That's a good question, IMHO, and I wonder how others would handle it.
As you've seen, there's no command to "continue" a loop, only exit it,
so the question becomes how to handle this in the clearest way. I'd
probably use a flag variable, like this...

For ip = 1 to 256

... ' do stuff
Dim connected as Boolean = False
Try
scope.Connect()
connected = True
Catch ex As Exception
End Try

if connected Then
For each os in searcher.Get()
...
Next
End If
Next
Nov 21 '05 #3

P: n/a
Continue will return in VS2005, however the common practice is to use a Goto
statement and a Label at the appopriate point ( assuming that a conditional
construct does not fit ).
For x=1 to 256

. . .

. . .

if SomeConditionIsTrue Then Goto ContinueHere

. . .

. . .

ContinueHere:
next



--

OHM ( Terry Burns )
. . . One-Handed-Man . . .
If U Need My Email ,Ask Me

Time flies when you don't know what you're doing

"David" <df*****@woofix.local.dom> wrote in message
news:slrncj42pv.lhn.df*****@woofix.local.dom...
On 2004-08-29, Microsoft <ma*@mas.com> wrote:
I have a for loops that goes from 1 to 256
I test the number and exception is thrown if there is an error.

How do I get the program to stop in the middle of the loop and go to the
next increment if an exception is thrown. I can only figure out how to exit the loop.


That's a good question, IMHO, and I wonder how others would handle it.
As you've seen, there's no command to "continue" a loop, only exit it,
so the question becomes how to handle this in the clearest way. I'd
probably use a flag variable, like this...

For ip = 1 to 256

... ' do stuff
Dim connected as Boolean = False
Try
scope.Connect()
connected = True
Catch ex As Exception
End Try

if connected Then
For each os in searcher.Get()
...
Next
End If
Next

Nov 21 '05 #4

P: n/a
* "Microsoft" <ma*@mas.com> scripsit:
I have a for loops that goes from 1 to 256
I test the number and exception is thrown if there is an error.

How do I get the program to stop in the middle of the loop and go to the
next increment if an exception is thrown. I can only figure out how to exit
the loop.


\\\
Currently, you can use a label and 'GoTo':

For i = 1 To 10
...
If...Then
GoTo NextIteration
End If
...
NextIteration:
Next i
///

In VB 2005 you can use 'Continue'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.