视觉工作室 |如果忘记在摘要中声明异常则通知

VisualStudio | Notify if forgotten to declare an Exception in the summary

我有以下FooClass。共有三种方法,其中两种可以抛出 exception。 如果仅在 HasException 方法中声明,则可以抛出 exception。 当您有大型项目时,您可能会忘记在摘要中描述 exceptions

如果我忘记声明一个exception,可以扫描解决方案吗?在我的示例中缺少四个 exceptions(声明)。

public class FooClass
{
    public FooClass()
    {
        HasException();

        HasNoException();

        HasChildException();
    }

    /// <summary>
    /// This method does not throw an <see cref="Exception"/>.
    /// </summary>
    public void HasNoException()
    {

    }

    /// <summary>
    /// This method is throwing an <see cref="Exception"/>.
    /// </summary>
    /// <exception cref="Exception">Is immediately thrown.</exception>
    public void HasException()
    {
        throw new Exception();
    }

    /// <summary>
    /// This method can throwing an <see cref="Exception"/> from a method it calls.
    /// </summary>
    public void HasChildException()
    {
        //throws an exception
        HasException();

        //throws also an exception
        int.Parse("foo");
    }
}

工具提示

这应该是描述方法的示例

/// <summary>
/// This method can throwing an <see cref="Exception"/> from a method it calls.
/// </summary>
/// <exception cref="Exception"></exception>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="FormatException"></exception>
/// <exception cref="OverflowException"></exception>
public void HasChildException()
{
    //throws an exception
    HasException();

    //throws also an exception
    int.Parse("foo");
}

执行此操作的一种方法(不是唯一的方法)是使用名为 "Exceptional" 的 Resharper 扩展。转到 Resharper > 扩展管理器 > 搜索 "Exceptional",安装并重新启动 Visual Studio。之后,所有可能抛出异常但未记录的地方都将带有下划线,并且使用 alt-enter 您将能够自动创建异常文档模板。对于您的示例,它会找到您提到的所有情况甚至更多(构造函数本身,它也可以抛出所有异常)。