I built some CodeExample:
static void Main(string[] args)
{
foreach (String hello in helloList)
{
DoSomething(hello);
}
}
public static void DoSomething(String hello)
{
try
{
//Some Code
}
catch (Exception exception)
{
Console.WriteLine(exception.Message);
Console.ReadKey();
}
}
I’m iterating through a List and sometimes it happens, that the program walks into the Catch. Now, the Program terminates, after Console.ReadKey(); – but what I want is, getting back to the foreach Loop and continue the work.. How can I achieve this?
From the Catch, I only need the Message..
EDIT:
OriginalCode:
static void Main(string[] args)
{
//Some unimportant code
StringCollection bilderUnterLink = HoleBildLinksVonWebseite(htmlInhaltUnterLink);
foreach (String bild in bilderUnterLink)
{
BildAbspeichern(bild);
}
}
public static void BildAbspeichern(String bildLink)
{
string speicherOrt = webseite + @"/" + bildLink;
string gueltigerBildLink;
if (bildLink.Contains("http://"))
{
gueltigerBildLink = bildLink;
}
else
{
gueltigerBildLink = "http://" + webseite + @"/" + bildLink;
}
if (!File.Exists(webseite + @"/" + Path.GetFileName(gueltigerBildLink)))
{
try
{
WebClient client = new WebClient();
client.DownloadFile(gueltigerBildLink, speicherOrt);
Console.WriteLine(String.Format("Bild gepeichert: " + "{0}", gueltigerBildLink));
}
catch (Exception exception)
{
Console.WriteLine(exception.Message);
Console.ReadKey();
}
}
}
I think thats enough important code.. And it terminates, I know it shouldn’t..
If it is terminating, I suspect the exception is simply not inside the
try; so… change that:Other notes:
Console.Errorfor error output; good practice (this writes tostderrrather thanstdout)ReadKey()completelyyou should use
usingonWebClient, since it isIDisposable, i.e.