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.
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. 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.
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. 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
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.