在c#代码中记录异常

本文关键字:记录 异常 代码 | 更新日期: 2023-09-27 18:06:54

我注意到在mscorlib.xml(从摘要生成的XML文件)中包含:

<member name="M:System.Console.ReadKey">
  <summary>Obtains the next character or function key pressed by the user. The pressed key is displayed in the console window.</summary>
  <returns>A <see cref="T:System.ConsoleKeyInfo" /> object that describes the <see cref="T:System.ConsoleKey" /> constant and Unicode character, if any, that correspond to the pressed console key. The <see cref="T:System.ConsoleKeyInfo" /> object also describes, in a bitwise combination of <see cref="T:System.ConsoleModifiers" /> values, whether one or more SHIFT, ALT, or CTRL modifier keys was pressed simultaneously with the console key.</returns>
  <exception cref="T:System.InvalidOperationException">The <see cref="P:System.Console.In" /> property is redirected from some stream other than the console.</exception>
  <filterpriority>1</filterpriority>
</member>

我对这个例子特别感兴趣:

<exception cref="T:System.InvalidOperationException">The <see cref="P:System.Console.In" /> property is redirected from some stream other than the console.</exception>

如何在代码中记录异常,以便它们最终出现在XML中?

在c#代码中记录异常

下面是一个例子:

/// <exception cref="ArgumentException">
/// Thrown when the <typeparamref name="TConcrete"/> is a type
/// that can not be created by the container.
/// </exception>

换句话说,与您在问题中已经显示的代码片段完全相同。

您可以在源代码的XML注释中添加带有<exception>标记的异常文档。例如:

///<summary>Summary of method/property/etc</summary>
///<returns>Returns a value</returns>
///<exception>Throws an exception</exception>