假设我在 C# 中有以下方法:
XslCompiledTransform myObject;
public void foo() {
try {
myObject.Transform(input, output);
} catch (???) {
}
}
Visual Studio 2012 中是否有一个菜单选项或功能可以自动为 XslCompiledTransform
中可能发生的异常生成所有 catch
block ?喜欢“生成捕获 block ”?
最佳答案
我个人认为这通常不是一个好习惯。在我看来,您应该只捕获您认为可能 抛出的异常。
这意味着在你的例子中:
- 你不应该在你的例子中捕获
ArgumentNullException
,但是在调用你的方法之前检查input
/output
不为 null - 我会捕获
IOException
而不是DirectoryNotFoundException
/FileNotFoundException
除非 你对这两种情况有不同的异常处理
请记住,只有当您有特定的异常处理相关联时,捕获特定的异常才有意义。如果只是“记录然后重新抛出”,那么不要捕获特定的异常。
编辑:我假设您正在寻找可以完成这项工作的工具。我不知道有这样的工具(您可能想查看 visualstudiogallery)。有趣的是,Exception Hunter ,一个来自 RedGate 的工具正在做这项工作,由于有趣的原因已经停止。看看:
With the release of .NET 4.0 and WPF, the number of exceptions that the CLR can throw was greatly increased, to the point of being overwhelming. The exclusions list can no longer cover all the unlikely exceptions that the CLR may throw. This means that, although Exception Hunter will provide accurate results, these results will include a long list of potential exceptions, most of which are nothing to worry about. In essence, the tool has become a lot less usable and makes your job harder than it should be. This goes against our ingeniously simple ethos, so we have decided to stop selling new licenses for the product.
关于c# - 用于自动为异常生成相关捕获 block 的 Visual Studio 功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21749947/