2

我是否记录由方法引发的异常开始由公共方法调用?根据我所阅读的内容,我应该尝试记录此类异常,这是有道理的。同时,很快就会证明“不可能”记录所有可以抛出的异常。

此外,除非我误解了如何检查 .NET 方法引发的异常,否则似乎 Microsoft 没有记录方法可以引发的所有异常。 XmlReader.Create()列出了四个可以抛出的异常:System.ArgumentNullExceptionSystem.Security.SecurityException和。我试图传入一个以“htp://”开头的uri,看看会抛出什么异常。我得到了一个,最终被.System.IO.FileNotFoundExceptionSystem.UriFormatExceptionSystem.NotSupportedExceptionSystem.Net.WebRequest.Create(..)

我是否正确,因为微软没有记录所有可以抛出的异常,还是我误解了某些东西。

在下面我自己的方法中,我会记录ArgumentOutOfRangeExceptionand NotValidSyndicationException,但我是否还应该记录上面提到的四个异常XmlReader.Create()System.NotSuppoedException, or (在指定的 uri 处没有端点时抛出)怎么样System.Net.WebException,这是另一个没有记录的异常,但必须“通过我的反复试验发现”。

public IEnumerable<SyndicationItem> GetItems(int count)
{
        if (count <= 0)
        {
            throw new ArgumentOutOfRangeException("count", "Count must be positive.");
        }

        IEnumerable<SyndicationItem> items;
        using (var rssReader = XmlReader.Create(this.uri))
        {
            var feedFormatter = new Rss20FeedFormatter();
            if (feedFormatter.CanRead(rssReader))
            {
                feedFormatter.ReadFrom(rssReader);
                items = feedFormatter.Feed.Items.Take(count);
            }
            else
            {
                throw new NotValidSyndicationException("The uri does not point to a valid syndication.");
            }
        }
        return items;
}
4

0 回答 0