Exiting a for loop

M

Microsoft

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
 
I

Imran Koradia

Well - a better idea would be to put all of the code after your End Try
statement within the Try-catch block. I assume you don't want to execute the
code thats after the 'end try' statement if an exception is thrown, right?
You could just have all that code within the try-catch block itself. The
rest of the code after scope.connect() will never execute whenever an
exception is thrown on the scope.connect() statement since it will jump to
catch statement whenever that happens.
Ofcourse, you could have exceptions thrown even after the scope.connect()
statement in which case you could first catch the specific exception that is
thrown when an error occurs on scope.connect() and then catch the remaining
exceptions.

I hope that made some sense :) and helps you a bit..
Imran.
 
E

Etienne Boucher

You should post that to the VB.NET newsgroup, since it's a language issue.
In C# there is the continue statement which are from the C and C++ roots of
the language. When encountering this statement, the execution flow by-passes
the remaining loop statement and goes to the next iteration.

Quick exemple in C#...

for (int i = 0; 5 > i; ++i)
{
if (3 == i)
continue;
Console.WriteLine(i);
}

The number 3 will not be printed.

Etienne Boucher
 
N

Nick Malik

In addition to the excellent answers so far:
You could set a flag variable in the catch block.

dim ErrorCaught as boolean
for (ip = 1 to 256)
ErrorCaught = false
try
' do stuff here
catch
' record the errror
ErrorCaught = true
end try
If Not ErrorCaught then
' do more stuff here
End If
Next

This is an older technique, but it works just fine.

--- Nick
 
J

Joshua Frank

This is the ONE place where I think GoTo is better than the alternative:

For i As Integer = 1 To 10
'code here...
if i = 3 then GoTo Continue

'code here...
Continue:
Next


Continue is a pretty fundamental need, and they're finally adding it in
VS 2005, but meanwhile I simulate it as shown above.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top