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

本文关键字:循环 一个 许多 异常 抛出异常 继续 | 更新日期: 2023-09-27 18:35:07

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

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 循环

上级:

怎么样

例如:方法 1 -> 方法3 -> 方法 2

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

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

  1. 你不能在尝试/捕获内部有yield return
  2. 如果你真的想这样做,
  3. 你可以这样做,但我真的不推荐这种方法。在我看来,异常应该在何时何地被抛出或应该重新抛出来处理。

    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
    }
    

你不能。一旦异常被抛回Method1Method2就无法继续。

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

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

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

正如其他答案所指出的,你不能"yield throw"一个例外。 一种解决方法是捕获异常并返回包含它的对象。 (例如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);
    }
}