我想知道什麼例外,我應該保護自己以防止使用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