We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Continuing a "For Each" loop when error occurs in "For " statement

d_melnyk
d_melnyk asked
on
Medium Priority
252 Views
Last Modified: 2012-05-06
Hi Folks:

    I am programming a recursive function to load a database table with all directories and files given a specific starting path. The code works, but hits a snag when I hit the "System Volume Information directory which is access denied. on a drive the line throwing the exception is the
"For each fileObject in dirinfo.getfilesysteminfos()" which starts the loop. A try catch around the loop will catch the error, but then any other items in the collection will not be processed as I am now "out" of the for each / next loop. How can I recover from the error being generated on that line - just need to write and error message to a log file and then continue the For Each / next until all items are done.

Any assistance will be greatly appreciated.
Best regards, Dave Melnyk

Dim fileobject as FileSystemInfo
        Dim dirInfo As New IO.DirectoryInfo(Resource & Folder)
         For Each fileobject In dirInfo.GetFileSystemInfos()
               ' process directories and files here ...
            
         Next

Open in new window

Comment
Watch Question

Top Expert 2007
Commented:
        For Each fileobject In dirInfo.GetFileSystemInfos()

               Try
               ' process directories and files here ...

               Catch
                    Continue For

               End Try
           
         Next

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Hi thanks for the reply ... unfortunately that does not work as the line which throws the exception is the one ABOVE the "try" in your example as I indicated in my question "...the line throwing the exception is the "For each fileObject in dirinfo.getfilesysteminfos()" ... Not sure if it is possible to separate out the actions being performed in that line to allow them to be enclosed by a try / catch structure.

regards, Dave Melnyk

Author

Commented:
Hi VBRocks ...
    Oops ... seemed I made a bit of a mistake setting up the catch - appears your solution is correct and will catch that error - seems strange though as the "try / catch" is within the loop and one would think that it would only catch errors in the code it encapsulates, not from the line above - seems strange ... but at least problem seems solved. Thanks again..


Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.