XML documentation of several exceptions for C #

I'm really looking for guidance on how to document a few exceptions in a public method inside 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 right to document these exceptions? Should I add one exception tag for each case or add only one for all parameters for which null values ​​are not valid?

 /// <exception cref="ArgumentNullException"> /// Thrown if p_Parameter1, p_Parameter2 /// or any element of p_Parameter2 are null</exception> 
+4
source share
2 answers

I would definitely group exceptions by type, i.e. Thrown if p_Parameter1, p_Parameter2 or any element of p_Parameter2 are null .

For reference, view the documentation on MSDN. example :

 ArgumentNullException | Either path, contents, or encoding is null. 
+4
source

MSDN is a good source to follow in this case and polling several functions there looks like they use one exception block and list the various parameters inside it.

Performing this method makes it easier for consumers of your code to know which exceptions can be caught, because viewing a list of individual exceptions is easier to find than a list containing duplicates.

+2
source

Source: https://habr.com/ru/post/1441802/


All Articles