c# - C# 的多个异常的 XML 文档

标签 c# visual-studio-2010 exception-handling xml-documentation

我实际上是在寻找指南,如何在 C#-DLL 中的公共(public)方法中记录多个异常。

例子:

/// <summary>
/// This method does something
/// </summary>
/// <param name="p_Parameter1">First parameter</param>
/// <param name="p_Parameter2">Second parameter</param>
/// <param name="p_Number">A number</param>
/// <exception cref="ArgumentNullException">
/// Thrown if p_Parameter1 is null</exception>
/// <exception cref="ArgumentNullException">
/// Thrown if p_Parameter2 is null</exception>
/// <exception cref="ArgumentNullException">
/// Thrown if any element of p_Parameter2 is null</exception>
/// <exception cref="ArgumentOutOfRangeException">
/// Thrown if p_Number is below or equal 0</exception>
/// <returns>A object</returns>
public static object DoSomething(
    object p_Parameter1, IList<object> p_Parameter2, 
    object p_Parameter3, int p_Number)
{
    if(p_Parameter1 == null)
        throw new ArgumentNullException(
            paramName:"p_Parameter1", 
            message:"Parameter is needed");
    if (p_Parameter2 == null)
        throw new ArgumentNullException(
            paramName: "p_Parameter2", 
            message: "Parameter is needed");

    for (int i = 0; i < p_Parameter2.Count; i++)
    {
        if(p_Parameter2[i] == null)
            throw new ArgumentNullException(
                paramName: String.Format("p_Parameter2[{0}]", i),
                message: "All elements have to be initialized");
    }

    if(p_Number < 0)
        throw new ArgumentOutOfRangeException(
            paramName: "p_Number", 
            message: "Parameter should be bigger then zero");

    var returnValue = new object();

    // do something where p_Parameter3 == null is allowed

    return returnValue;
}

记录这些异常的方式是否正确?我应该为每种情况添加一个异常标记,还是应该为所有不允许空值的参数只添加一个异常标记?

/// <exception cref="ArgumentNullException">
/// Thrown if p_Parameter1, p_Parameter2
/// or any element of p_Parameter2 are null</exception>

最佳答案

我肯定会按类型对异常进行分组,即 Thrown if p_Parameter1, p_Parameter2 或 p_Parameter2 的任何元素为空

作为引用,请查看 MSDN 上的文档。一个example :

ArgumentNullException   |   Either path, contents, or encoding is null.

关于c# - C# 的多个异常的 XML 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13049427/

相关文章:

c# - IOrganizationService 更新实体的正确方法

c# - 在 C# 中使用自由泛型类型参数模拟委托(delegate)

c# - 如何在 WPF DataGrid 中按列搜索?

c# - C# 中的表单/类在编码时未在 Intellisense 中检测到

c# - 无法将类型 `double[]` 隐式转换为 `System.Collections.Generic.List<double>`

visual-studio-2010 - 如何强制我的 TFS 2010 Build 构建解决方案中的所有项目以在调试或 Release模式下构建?

visual-studio-2010 - 使用 Visual Studio 2010、MFC 和 FFmpeg 构建

c# - 为什么 Dispatcher.BeginInvoke 为 ThreadStart 解包 TargetInvocationException 而不是为 Action 解包?

c# - 在 C# 中传播在 finally block 中抛出的异常而不丢失 catch block 中的异常的最佳实践是什么?

c# - 检查内部异常的最佳方法?