我想知道什么例外,我应该保护自己以防止使用WebClient.DownloadString
。异常处理WebClient.DownloadString的正确方式
下面是我目前使用它的方式,但我相信你们可以建议更好的更强大的异常处理。
例如,把我的头顶部:
- 没有互联网连接。
- 服务器返回了404.
- 服务器超时。
什么是处理这些情况并抛出异常到UI的首选方法?
public IEnumerable<Game> FindUpcomingGamesByPlatform(string platform)
{
string html;
using (WebClient client = new WebClient())
{
try
{
html = client.DownloadString(GetPlatformUrl(platform));
}
catch (WebException e)
{
//How do I capture this from the UI to show the error in a message box?
throw e;
}
}
string relevantHtml = "<tr>" + GetHtmlFromThisYear(html);
string[] separator = new string[] { "<tr>" };
string[] individualGamesHtml = relevantHtml.Split(separator, StringSplitOptions.None);
return ParseGames(individualGamesHtml);
}
请参阅编辑。 – 2011-05-14 01:22:41
更新了我的回答 – 2011-05-14 01:26:47
谢谢托马斯,我想我是问如何冒泡异常错误。 :) – 2011-05-14 01:33:14