के लिए सही तरीके से निपटने का अपवाद मैं सोच रहा था कि WebClient.DownloadString
का उपयोग करते समय मुझे अपने आप को कौन से अपवादों की रक्षा करनी चाहिए।WebClient.DownloadString
यहां मैं वर्तमान में इसका उपयोग कैसे कर रहा हूं, लेकिन मुझे यकीन है कि आप बेहतर मजबूत अपवाद हैंडलिंग का सुझाव दे सकते हैं।
उदाहरण के लिए, मेरे सिर के ऊपर से:
- कोई इंटरनेट कनेक्शन।
- सर्वर ने 404 लौटाया।
- सर्वर का समय समाप्त हो गया।
इन मामलों को संभालने और यूआई को अपवाद फेंकने का पसंदीदा तरीका क्या है?
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);
}
कृपया संपादित करें देखें। –
मेरा उत्तर अपडेट किया गया –
धन्यवाद थॉमस, मुझे लगता है कि मैं अपवाद त्रुटि को बबल करने के लिए कह रहा था। :) –