#913 – How to Document that a Method Can Throw an Exception
August 21, 2013 3 Comments
You can use XML Documentation to document the fact that a method can throw an exception. You use the Exception tag to indicate the type of exception that can be thrown. You can also provide a comment related to the exception, which will show up in an XML Documentation file that you generate.
/// <summary> /// Print somebody's name /// </summary> /// <param name="name">Name to be printed</param> /// <exception cref="ApplicationException">Will throw exception if name is Mortimer</exception> /// <exception cref="JustBecauseException">I throw this exception on Tuesdays</exception> static void PrintMyName(string name) { if (name.ToLower().Equals("mortimer")) throw new ApplicationException("I don't like the name Mortimer"); if (DateTime.Now.DayOfWeek == DayOfWeek.Tuesday) throw new JustBecauseException("Tuesday is my day off"); Console.WriteLine(string.Format("Your name is {0}", name)); }
Intellisense will now include a list of the exceptions that might be thrown, when displaying the method’s name.
If you generate a XML Documentation file, it will also include this information.