简体   繁体   English

C#的多个异常的XML文档

[英]XML-Documentation of multiple exceptions for C#

I'm actually searching for a guidline, how to document multiple exceptions in a public method inside a C#-DLL. 我实际上是在寻找指南,如何在C#-DLL内的公共方法中记录多个异常。

example: 例:

/// <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;
}

Is it the right way to document those exceptions? 是记录这些例外的正确方法吗? Should I add one exception-tag for each case, or should I add only one for all Parameters for which null-values ar not allowed? 我应该为每种情况添加一个例外标记,还是应该为所有不允许使用空值的参数添加一个例外标记?

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

I would definitely group the exceptions by type, ie Thrown if p_Parameter1, p_Parameter2 or any element of p_Parameter2 are null . 我肯定会按类型对异常进行分组,即, Thrown if p_Parameter1, p_Parameter2 or any element of p_Parameter2 are null该异常。

As a reference, look at the documentation at MSDN. 作为参考,请查看MSDN上的文档。 An example : 一个例子

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

MSDN is a good source to emulate in this case, and taking a survey of a few functions there it looks like they tend to use one exception block and enumerate the different parameters inside of it. 在这种情况下,MSDN是一个很好的模拟来源,并且对那里一些 功能进行了调查,看来它们倾向于使用一个exception块并枚举其中的不同参数。

Doing it this way makes it easier for consumers of your code to know what exceptions to possibly catch because seeing a list of distinct exceptions is easier to grok than a list that contains duplicates. 这样,代码的使用者就可以更轻松地知道可能捕获哪些异常,因为与包含重复项的列表相比,查看不同异常的列表更容易出错。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM