简体   繁体   English

如何继续foreach循环

[英]how to continue a foreach loop

I have the for each loop below and would like to know how i would be able to continue this after an exception has been thrown so that it goes onto the next next array index, and the system doesn't fail. 我在下面有for每个循环,想知道在引发异常后如何继续执行此操作,以便将其转到下一个下一个数组索引,并且系统不会失败。

try
{
//making name array and other checks 
    foreach (string s in namearry)
    {
        var timelineData = oAuthTwitterWrapper.GetMyTimeline(s);
        TwitterData.TimeLineData(timelineData, s, int.Parse(dr["ClientId"].ToString()));
        //  var followersId = oAuthTwitterWrapper.GetFolowersId(s);
        // var loc = oAuthTwitterWrapper.GetFolowersLoc(followersId);
        //  TwitterData.Follower(loc, s);
    }
}
catch(Exception ex)
{
    //logging exception 
}

Ideally i would try to avoid all the exceptions. 理想情况下,我会尽量避免所有例外情况。 In your case you can handle the exception within the foreach loop. 在您的情况下,您可以在foreach循环中处理异常。 In the following examples i have added the necessary checks to avoid exception occuring in first place. 在下面的示例中,我添加了必要的检查,以避免首先发生异常。 like this 像这样

foreach (string s in namearry)
{
    try
    {
        var timelineData = oAuthTwitterWrapper.GetMyTimeline(s);
        if(timelineData!=null)
        {
             int clientID;
             if(int.TryParse(dr["ClientId"].ToString(), out clientID))
             {
                  TwitterData.TimeLineData(timelineData, s, clientID);            
             }
        }
    }
    catch(Exception exp)
    {
        //do logging here.
    }
}

You cannot, you broke out with an exception, instead move the try/catch inside the loop. 您不能,因为异常而爆发,而是在循环内移动try / catch。

foreach (string s in namearry)
{
    try {
        //making name array and other checks 
        var timelineData = oAuthTwitterWrapper.GetMyTimeline(s);
        TwitterData.TimeLineData(timelineData, s, int.Parse(dr["ClientId"].ToString()));
        //  var followersId = oAuthTwitterWrapper.GetFolowersId(s);
        // var loc = oAuthTwitterWrapper.GetFolowersLoc(followersId);
        //  TwitterData.Follower(loc, s);
    }
    catch(Exception ex) {
        //logging exception 
    }
}

将您的try-catch语句放入循环,并在catch块中使用Continue关键字。

Put the try inside the foreach, rather than outside. try 放在 foreach 内部 ,而不是外部。 Or, if you need it outside, put another one inside, that handles exceptions. 或者,如果您在外部需要它,则在内部放置另一个以处理异常。

try{
//making name array and other checks, that may trigger exceptions
    foreach (string s in namearry)
    {
        try
        {
            var timelineData = oAuthTwitterWrapper.GetMyTimeline(s);
            TwitterData.TimeLineData(timelineData, s, int.Parse(dr["ClientId"].ToString()));
            //  var followersId = oAuthTwitterWrapper.GetFolowersId(s);
            // var loc = oAuthTwitterWrapper.GetFolowersLoc(followersId);
            //  TwitterData.Follower(loc, s);
        }
        catch(Exception ex)
        {
            //logging exception: this will override the outer handler, which will not be called.
        }
    }
}
catch(Exception ex){
    //logging exception
    //exceptions raised before entering the foreach are handled here
}

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

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