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

Posted on 2009-02-17
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 ...



Open in new window

Question by:d_melnyk
    LVL 27

    Accepted Solution

            For Each fileobject In dirInfo.GetFileSystemInfos()

                   ' process directories and files here ...

                        Continue For

                   End Try

    Author Comment

    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 Comment

    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..


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
    It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now