c# - 用于自动为异常生成相关捕获 block 的 Visual Studio 功能?

标签 c# visual-studio exception visual-studio-2012

假设我在 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/

相关文章:

c# - EF Core - 用于简单 ValueObject 的 ValueConverter 或 OwnedType?

c# - 引发事件或返回 bool 值?

visual-studio - 如何从 docker-compose.ci.build.yml 文件构建和推送容器

visual-studio - Visual Studio 如何连接到存储库位于 GitHub 上的 Azure DevOps 工作区?

java - Java “try without catch”和 “catch without try”

c# - 在C#中执行mysqldatareader时为"The given key was not present in the dictionary"

c# - 使用 linq 从 select new 返回多行

c++ - VS2015 : How to hide the 'References' folder in solution explorer?

javascript - 当 check() 抛出 Match.Error 时如何向客户端抛出错误消息?

oracle - 当其他人然后异常处理