【发布时间】:2014-05-15 18:22:29
【问题描述】:
我正在尝试确定如何捕获异常我得到的是Object reference not set to an instance of an object.
有没有更好的方法来捕捉异常并向用户展示异常的原因?
baseUrl = "my url....";
try
{
HtmlWeb hw = new HtmlWeb();
HtmlDocument docSRC = hw.Load(baseUrl);
//if (docSRC.DocumentNode.SelectNodes("//img/@src").Count > 0)
//{
//}
foreach (HtmlNode link in docSRC.DocumentNode.SelectNodes("//img/@src"))
{
HtmlAttribute att = link.Attributes["src"];
srcTags.Add(att.Value);
}
}
catch (Exception ex)
{
//catch reason for exception....
}
【问题讨论】:
-
我希望 HtmlAgilityPack 如果找不到元素就返回 null 。这比预先检查元素是否存在或抛出异常要容易得多。
标签: c# error-handling html-agility-pack