简体   繁体   English

如何引发异常并继续循环(或如何从一个循环引发许多异常)

[英]How to throw an exception and continue looping (or how to throw many exception from one loop)

I have two method and for me need to generate exception from one to another some like this 我有两种方法,对我来说,需要像这样从一个到另一个生成异常

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 */
       }            
    }
 }

How to delegate exception from Method 2 to Method 1 and continue foreach loop in method 2 如何将异常从方法2委派给方法1并继续方法2中的foreach循环

UPD: UPD:

And how about 那怎么样

for example : Method 1 -> Method3 -> Method2 -> method 2 return exception in method 1 例如:方法1->方法3->方法2->方法2返回方法1中的异常

UPD2: to UPD 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. You can't have yield return inside try/catch. 您不能在try / catch中获得yield return
  2. You can do it this way if you really want to do it but I don't really recommend this approach. 如果您确实想这样做,可以这样做,但是我不建议这样做。 Exceptions should be handled when and where they are thrown or should be re-thrown in my opinion. 我认为应在何时何地抛出异常,或将异常重新抛出。

     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 } 

You cannot. 你不能。 Once an exception is thrown back to Method1 , Method2 cannot continue. 一旦有异常抛出回到Method1Method2无法继续。

What you can do, however, is give Method2 a callback function that handles the exceptions. 但是,您可以做的是为Method2一个处理异常的回调函数。

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

Now Method1 can pass a function that gets an exception, and returns whether it has handled that exception. 现在, Method1可以传递一个获取异常的函数,并返回它是否已处理该异常。 If it has, the loop continues. 如果有,则循环继续。

As noted in the other answers, you can't " yield throw " an exception. 如其他答案所述,您不能“ yield throw ”异常。 One workaround would be to catch the exception and return an object that includes it. 一种解决方法是捕获异常并返回包含该异常的对象。 (eg a KeyValuePair<int, Exception> ) (例如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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM