C#中发生异常后如何继续

时间:2011-01-12 05:48:18

标签: c# asp.net

    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();

}

输出

处理C:\ newFolder \ fileA.txt

由于对象的当前状态,操作无效。

但我需要输出为:

处理C:\ newFolder \ fileA.txt

由于对象的当前状态,操作无效。

由于对象的当前状态,操作无效。

处理C:\ newFolder \ fileD.txt

请帮助.............

3 个答案:

答案 0 :(得分:4)

SomeMethodThatMightThrow内的try/catch内执行foreach

示例:

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

答案 1 :(得分:0)

静态字符串SomeMethodThatMightThrow(string s)
        {
           尝试
           {
               if(s [4] =='C')
                   抛出新的InvalidOperationException();
                return @“C:\ newFolder \”+ s;
            }
            catch(InvalidOperationException e)
            {
               返回e.Message;
             }
            catch(Exception ex)
            {
                  返回“其他错误”;
             }

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

}

答案 2 :(得分:0)

我猜你的操作结果应包含实际结果和异常(如果发生这种情况) - 例如,

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();

我使用OperationWrapper假设你可能有很多这样的功能,否则你可能会改变实际的功能。