如何抛出异常并继续循环(或如何从一个循环中抛出许多异常)

时间:2014-12-22 11:09:06

标签: c# exception exception-handling

我有两种方法,对我来说需要从一个到另一个生成异常,就像这样

public void Method1()
{ 
    try
    {  
        Method2(1);
    }
    catch(Exception e )
    {
        SendEmail (/* some message */)
    }
}

public IEnumerable<int> Method2(int id)
{
    foreach (var i in col)
    {
        try
        { 
            /*Do some stuff*/ 
            yield return i 
        }
       catch
       {
           /* delegate this exception to Method1 and continue foreach loop */
       }            
    }
 }

如何将方法2中的异常委托给方法1并继续方法2中的foreach循环

UPD:

怎么样

例如:方法1 - &gt;方法3 - &gt;方法2 - &gt;方法2在方法1中返回异常

UPD2:UPD

   /*Call*/
        var i = new List<int>() {0, 0, 0, 0, 0, 0, 7, 0, 9};
        Calc(i, SendMessage);


   public static void SendMessage(Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }

    public static double Calc(List<int> list, Action<Exception> callback)
    {
        var a = 0.00;
        foreach (var i in list)
        {
            try
            {
                a = Calc1(i);/*if here (double)7 / i it's just works but in how to include in method*/
            }
            catch (Exception ex)
            {
                callback(ex);
            }
        }
        return a;
    }

    public static double Calc1(int i)
    {
        var a = 0.00;
        a = (double)7 / i;
        return a;
    }

3 个答案:

答案 0 :(得分:8)

  1. 你在try / catch中没有yield return
  2. 如果你真的想这样做,你可以这样做,但我不是真的推荐这种方法。应该在他们被抛出的时间和地点处理异常,或者在我看来应该重新抛出异常。

    public void Method1()
    {
        Method2(1, ex => SendEmail(ex));
    }
    
    public IEnumerable<int> Method2(int id, Action<Exception> callback)
    {
        foreach (var i in new List<int>())
        {
            try
            {
                /*Do some stuff*/
            }
            catch(Exception ex)
            {
                callback(ex);
            }
    
            yield return i;
        }
    }
    
    private void SendEmail(Exception ex)
    {
        // blah
    }
    

答案 1 :(得分:2)

你做不到。一旦异常被抛回Method1Method2将无法继续。

但是,您可以做的是给Method2一个处理异常的回调函数。

public IEnumerable<int> Method2(Func<Exception, bool> handler)
{
    foreach (var item in collection)
    {
        try
        {
            // ...
        }
        catch (Exception ex)
        {
            if (!handler(ex))
                throw;
        }
    }
}

现在Method1可以传递一个获取异常的函数,并返回它是否已处理该异常。如果有,则循环继续。

答案 2 :(得分:1)

如其他答案中所述,您不能{&#34; yield throw&#34;一个例外。一种解决方法是捕获异常并返回包含它的对象。 (例如KeyValuePair<int, Exception>

public void Method1()
{
    foreach(var i in Method2(1))
    {
        if (i.Value == null)
        {
            // No Exception Thrown
        }
        else
        {
            // Exception Thrown
            SendEmail(); // Send a message
        }
    }       
}

public IEnumerable<KeyValuePair<int, Exception>> Method2(int id)
{
    List<int> col = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    Exception exception;

    foreach (var i in col)
    {
        exception = null;
        try
        { 
            if ((i % 2) == 1)
            {
                throw new Exception("Test" + i);
            }
            /*Do some stuff*/
        }
        catch (Exception ex)
        {
            exception = ex;
        }

        yield return new KeyValuePair<int, Exception>(i, exception);
    }
}