How to continue after exception occurred in C#
- by Manisha
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.............