6

foreach の実行中に例外をキャッチした場合、実行を続行できますか?

foreach (string d in Directory.GetDirectories(path))
    {
        try
        {
            foreach (string f in Directory.GetFiles(path))
            {
                //do something
            }
            //do something
        }
        catch
        {
           // If there is an error in the foreach, I want it to keep on going
        }
    }

foreachがすべてのファイルを取得する前に突然終了するため、これを求めています。

4

3 に答える 3

9

単に行う:

foreach (string d in Directory.GetDirectories(path))
{
        foreach (string f in Directory.GetFiles(path))
        {
            try
            {
                //do some thing
            }
            catch
            {
               // If there is an error on the foreach, I want it to keep on going to search another one
            }
        }
        //do some thing
}
于 2012-10-07T03:12:05.170 に答える
2
foreach (string d in Directory.GetDirectories(path)) {
    foreach (string f in Directory.GetFiles(path)) {
        try {
            //do some thing
        } catch { /* log an error */ }
    }

    try {
        //do some thing
    } catch { /* log an error */ }
}
于 2012-10-07T03:11:21.933 に答える