Ответ 1
Вы можете поймать WebException:
public static string GetPageHTML(string link)
{
try
{
using (WebClient client = new WebClient())
{
return client.DownloadString(link);
}
}
catch (WebException ex)
{
var statusCode = ((HttpWebResponse)ex.Response).StatusCode;
return "An error occurred, status code: " + statusCode;
}
}
Конечно, было бы уместнее поймать это исключение в вызывающем коде и даже не попытаться разобрать html вместо того, чтобы помещать try/catch в саму функцию.