我的C# windows窗体应用程序从url下载一个简短的字符串。它解析字符串并显示信息。它完全是用来做这个的。一种弹出式程序。我使用的是:
using (WebClient wc = new WebClient())
{
string json = wc.DownloadString(url);
//parse the string
//show windows form with the gathered data
this.Show()
}我的问题是,当没有互联网连接时,会弹出未处理的异常错误。我认为我需要防止这个错误消息,只需要保持程序运行,直到连接恢复。如何防止消息并保持其运行?
发布于 2016-07-20 19:30:16
使用try .. catch块结构,如
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 }
}https://stackoverflow.com/questions/38480047
复制相似问题