How to continue after exception occurred in C#

Posted by Manisha on Stack Overflow See other posts from Stack Overflow or by Manisha
Published on 2011-01-12T05:48:17Z Indexed on 2011/01/12 5:53 UTC
Read the original article Hit count: 230

Filed under:
|

static string SomeMethodThatMightThrow(string s) { if (s[4] == 'C') throw new InvalidOperationException(); return @"C:\newFolder\" + s;

    }
    static void Main(string[] args)
    {
       string[] files = { "fileA.txt", "B.txC", "fileC.txt","fileD.txt" };

    var exceptionDemoQuery =
        from file in files
        let n = SomeMethodThatMightThrow(file)
        select n;
    try
    {
        foreach (var item in exceptionDemoQuery)
        {
            Console.WriteLine("Processing {0}", item);
        }
    }

    catch (InvalidOperationException e)
    {
        Console.WriteLine(e.Message);

    }

    Console.WriteLine("Press any key to exit");
    Console.ReadKey();

}

Output is

Processing C:\newFolder\fileA.txt

Operation is not valid due to the current state of the object.

But i need the Output as:

Processing C:\newFolder\fileA.txt

Operation is not valid due to the current state of the object.

Operation is not valid due to the current state of the object.

Processing C:\newFolder\fileD.txt

Please help in this.............

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET