简体   繁体   中英

How to prevent for loop from stopping when a 404 occurs?

I have multiple links and I want to process all of them when the start button is pressed. If one of the links returns a 404, I want to skip it and move to the next one.

My current code is the following:

try
            {
                foreach (string s in txtInstagramUrls.Lines)
                {
                    if (s.Contains("something"))
                    {
                        using (WebClient wc = new WebClient())
                        {
                            Match m = Regex.Match(wc.DownloadString(s), "(?<=og:image\" content=\")(.*)(?=\" />)", RegexOptions.IgnoreCase);
                            if (m.Success)
                            {
                                txtConvertedUrls.Text += m.Groups[1].Value + Environment.NewLine;
                            }
                        }
                    }
                }
            }
            catch(WebException we)
            {
                if(we.Status == WebExceptionStatus.ProtocolError && we.Response != null)
                {
                    var resp = (HttpWebResponse)we.Response;
                    if (resp.StatusCode == HttpStatusCode.NotFound)
                    {
                        continue;
                    }
                }
                throw;
            }

The error shows on continue; saying No enclosing loop out of which to break or continue . I am not sure how to proceed from here. Any help is appreciated.

That is because when an exception is thrown, you have already left the foreach block and you are trying to continue but there is no loop to continue at that point. Here is your code simplified to show this:

try {
   foreach( string s in txtInstagramUrls.Lines ) {

   }
}
catch( WebException we ) {
   // There is no loop here to continue
}

You need to put the try inside the loop:

foreach( string s in txtInstagramUrls.Lines ) {
   try {
      // do something
   }
   catch( WebException we ) {
      continue;
      throw;
   }
}

So you need to change your code to following:

foreach( string s in txtInstagramUrls.Lines ) {
   try {
      if( s.Contains( "something" ) ) {
         using( WebClient wc = new WebClient() ) {
            Match m = Regex.Match( wc.DownloadString( s ), "(?<=og:image\" content=\")(.*)(?=\" />)", RegexOptions.IgnoreCase );
            if( m.Success ) {
               txtConvertedUrls.Text += m.Groups[ 1 ].Value + Environment.NewLine;
            }
         }
      }
   }
   catch( WebException we ) {
      if( we.Status == WebExceptionStatus.ProtocolError && we.Response != null ) {
         var resp = ( HttpWebResponse ) we.Response;
         if( resp.StatusCode == HttpStatusCode.NotFound ) {
            continue;
         }
      }
      throw;
   }

}

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.

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