0

My C# windows forms app downloads a short string form an url. It parses the string and shows info. It is used purely for this. Sort of a pop up program. I am using this:

using (WebClient wc = new WebClient())
{
string json = wc.DownloadString(url);
//parse the string
//show windows form with the gathered data 
this.Show()
}

My problem is, when there is no internet connection, unhandled exception error pops up. I think that I need to prevent this error message and just keep the program rolling until the connection is back up. How to prevent the message and keep it running?

  • There is a well known way to check if the internet connection is available (albeit not always correct) http://stackoverflow.com/questions/13625304/check-internet-connection-availability-in-windows-8 – Steve Jul 20 '16 at 11:34

1 Answers1

0

Using a try .. catch block construct like

using (WebClient wc = new WebClient())
{
try {
string json = wc.DownloadString(url);
//parse the string
//show windows form with the gathered data 
this.Show()
}
catch(exception ex) { //Log the exception with datetime }
}
Rahul
  • 76,197
  • 13
  • 71
  • 125
  • catch should prevent the message? how to log the exception? i am quite new with all this – Jonas Kazlauskas Jul 20 '16 at 11:38
  • @JonasKazlauskas, try searching in Google you will get million of samples. – Rahul Jul 20 '16 at 11:42
  • @Rahul it looks like the application should be catching WebException, instead of general Exception. That way application could return a response with a message, that there is no internet connection. – Mantas Marcinkus Jul 20 '16 at 12:11