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);
}
편집을 참조하십시오. –
updated my answer –
감사합니다. Thomas, 예외 오류를 버블 링하는 방법을 묻는 것 같아요. :) –