【问题标题】:How to prevent webexception message?如何防止 webexception 消息?
【发布时间】:2016-07-20 11:28:45
【问题描述】:

我的 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()
}

我的问题是,当没有互联网连接时,会弹出未处理的异常错误。我认为我需要防止出现此错误消息并保持程序滚动直到连接恢复。如何阻止消息并使其保持运行?

【问题讨论】:

标签: c# winforms exception exception-handling


【解决方案1】:

使用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 }
}

【讨论】:

  • catch 应该阻止消息吗?如何记录异常?我对这一切都很陌生
  • @JonasKazlauskas,尝试在 Google 中搜索您将获得数百万个样本。
  • @Rahul 看起来应用程序应该捕获 WebException,而不是一般的异常。这样,应用程序可以返回一条响应消息,即没有互联网连接。
猜你喜欢
  • 1970-01-01
  • 2014-06-09
  • 1970-01-01
  • 2016-01-13
  • 2016-10-13
  • 1970-01-01
  • 2014-06-10
  • 2018-06-14
  • 1970-01-01
相关资源
最近更新 更多