foreach loop stays in infinite loop when an exception occurs.

My code reads a list of files from one directory and moves them to another directory. The problem here is that if an exception occurs, then the code get's stuck in an infinite loop. Meaning the value stored in fileName stays the same, and is not updated(incremented) with the next string value in the fileList.

The exception will be caused if someone has the file open, or locked by another application.

What I need to do is to gracefully skip over the fileName that is locked up, or in use by another application. I suppose this can be done by somehow forcing fileName to point to the next value in the fileList Array.

Does anyone know how this can be done

string[] fileList = Directory.GetFiles(SourceDirectory); // SourceDirectory is a string that points to a specific directory on the hard drive

foreach (string fileName in fileList) {

try {

// C# code to move a file

}

catch(Exception ex) { throw ex; // if file is open and can not be moved then throw the exception }

}



Answer this question

foreach loop stays in infinite loop when an exception occurs.

  • Yousuf Khan

    string[] fileList = Directory.GetFiles();

    foreach( string file in fileList )

    {

    try{ // actions }

    catch{ // log the file I missed and continue }

    }

    This will ignore any errors occur for a given file and continue in the loop.



  • Sid007

    I still don't see how you're getting an infinite loop.  A foreach loop should never loop infinitely unless the enumerator is broken and returns an infinite number of results (which is doubtful here since you are using the array's enumerator).  Are you sure there isn't some inner loop or other construct in your foreach statement that is preventing it from moving on to the next iteration of the loop
  • Chunda

    You'll have to give us more code. The sample you've given so far will not cause an infinite loop; it will case the loop to be aborted with the thrown exception.

    BTW, it's best practice to only catch the exceptions that you can safely handle. Catching Exception is not wise as you can catch any number of exceptions that you can't recover from (like OutOfMemoryException, StackOverFlowException, etc.)

    Also, if you want to rethrow the current exception simply use "throw;". Using "throw exception;" causes the exceptions stackframe to get modified and higher-up code will not be able to figure out where the original exception came from.



  • Tommmy77

    My catch statement actually writes information to an error log. the catch statement actually looks like the following. I was in a hurry to make a small example of the problem and just put the throw statement in there. I know what you mean though about re-throwing the exception. So my mistake on posting the wrong catch code I am using.

    catch (Exception ex)

    {

    errLog.WriteEntry(ex.Message , EventLogEntryType.Error);

    }


  • N_G

    Instead of rethrowing the exception, why not simply use the continue statement
  • hollander67

    are you moving the files that you are reading in to the Source Directory your also pulling from because that is the only way i can see the foreach becoming a infinite loop.
  • Kishanb

    So, you're not rethrowing the exception

  • foreach loop stays in infinite loop when an exception occurs.