如何明确方法可以抛出异常?

bur*_*1ce 6 c# coding-style

我正在创建的方法之一抛出异常.什么是显示(在代码或注释中)我的方法可能抛出异常的最清晰的方式,因此try {}和catch {}需要应用于我的方法.

谢谢!

And*_*are 24

在方法的文档注释中使用<exception/>标记:

/// <summary>Does Foo</summary>
/// <exception cref="System.ArgumentNullException">
/// Thrown when bar is null.
/// </exception>
public void Foo(Bar bar) 
{ 

}
Run Code Online (Sandbox Code Playgroud)

使用<exception/>标记的一个好处是Visual Studio将此信息包含在方法信息工具提示中,如下所示:

http://i32.tinypic.com/2e1gi6u.jpg