Manisha
Manisha

Reputation: 17

How to continue after exception occurred in C#

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

Upvotes: 2

Views: 4077

Answers (3)

VinayC
VinayC

Reputation: 49225

I guess your result from operation should contain both actual result and exception (in case that happens) - for example,

static Func<string, Tuple<string, Exception>> OperationWrapper(Func<string, string> op)
{ 
   return s => { 
       try
       { 
          return Tuple.Create<string, Exception>(op(s), null);
       }
       catch(Exception ex)
       {
          return Tuple.Create<string, Exception>(null, ex);
       }
   };
}

// goes rest of the code except changes shown below
...

var wrapper = OperationWrapper(SomeMethodThatMightThrow);
var exceptionDemoQuery =
        from file in files
        let n = wrapper(file)
        select n;

        foreach (var item in exceptionDemoQuery)
        {
            if (item.Item2 == null)
            {
                 Console.WriteLine("Processing {0}", item.Item1);
            }
            else
            { 
                 // we have exception
                 Console.WriteLine(item.Item2.Message); 
            }
        }
    Console.WriteLine("Press any key to exit");
    Console.ReadKey();

I have use OperationWrapper assuming that you may have many such functions otherwise you may change the actual function itself.

Upvotes: 0

leppie
leppie

Reputation: 117310

Perform SomeMethodThatMightThrow wrapped in a try/catch within the foreach .

Example:

var exceptionDemoQuery =
    from file in files
    select file;

foreach (var item in exceptionDemoQuery)
{
  try
  {
    Console.WriteLine("Processing {0}", item);
    var n = SomeMethodThatMightThrow(item);
  }
  catch (Exception ex)
  {
    Console.WriteLine(e.Message);
  }
}

Upvotes: 3

Rajesh
Rajesh

Reputation: 115

static string SomeMethodThatMightThrow(string s)
{
try
{
if (s[4] == 'C')
throw new InvalidOperationException();
return @"C:\newFolder\" + s;
}
catch (InvalidOperationException e)
{
return e.Message;
}
catch(Exception ex)
{
return "other error";
}

    }   
    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;   

        foreach (var item in exceptionDemoQuery)   
        {   
            Console.WriteLine("Processing {0}", item);   
        }   



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

}

Upvotes: 0

Related Questions