Re: Exiting a for loop

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Nick Malik (nickmalik_at_hotmail.nospam.com)
Date: 09/01/04


Date: Wed, 01 Sep 2004 06:55:01 GMT

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

"Microsoft" <mas@mas.com> wrote in message
news:u5SSosHjEHA.1644@tk2msftngp13.phx.gbl...
> 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
>
>



Relevant Pages

  • Re: The process cannot access the file (in Windows2003 only)
    ... Dim sr As StreamReader ... catch ex as exception ... Break out of the loop ...
    (microsoft.public.dotnet.framework)
  • Help with executing access query
    ... I have written 6 Queries in an Access DB, which are executed in a For ... Dim cmd As New OleDbCommand ... Each...Next loop is exited and exception is thrown. ...
    (microsoft.public.dotnet.general)
  • Re: Looping through Query to create multiple sheets in excel- Just need the loop
    ... I always get to this point and I can't figure out how to loop through ... I have put the string in the query at the bottom. ... Optional strWorkSheet As String, Optional strRange As ... Dim objXLSheet As Object 'Excel.Worksheet ...
    (microsoft.public.access.forms)
  • Re: vbs to run thru list of files
    ... the plan for a script should contain the spots where to insert ... 04 Dim objFSO, objFl ... Dim oFile, sSIZ, ... ... What about variables used in a loop: ...
    (microsoft.public.scripting.vbscript)
  • Re: vbs to run thru list of files
    ... 04 Dim objFSO, objFl ... Dim oFile, sSIZ, ... ... What about variables used in a loop: ... b Index außerhalb des gültigen Bereichs ...
    (microsoft.public.scripting.vbscript)