Visual Studio:如何中断处理的异常?

时间:2020-03-06 14:33:31  来源:igfitidea点击:

我希望Visual Studio在发生处理的异常时中断(即,我不只是想看到"第一次机会"消息,我想调试实际的异常)。

例如我希望调试器在异常处中断:

try
{
   System.IO.File.Delete(someFilename);
}
catch (Exception)
{
   //we really don't care at runtime if the file couldn't be deleted
}

我遇到了有关Visual Studio.NET的这些说明:

1) In VS.NET go to the Debug Menu >>
  "Exceptions..." >> "Common Language
  Runtime Exceptions" >> "System" and
  select "System.NullReferenceException"
  
  2) In the bottom of that dialog there
  is a "When the exception is thrown:"
  group box, select "Break into the
  debugger" 
  
  3) Run your scenario. When the
  exception is thrown, the debugger will
  stop and notify you with a dialog that
  says something like: 
            "An exception of type "System.NullReferenceException" has
  been thrown. 
            [Break] [Continue]" 
  
  Hit [Break]. This will put you on the
  line of code that's causing the
  problem.

但是它们不适用于Visual Studio 2005("调试"菜单上没有"例外"选项)。

有谁知道在Visual Studio中的"当引发异常时"组框以及"闯入调试器"选项中可以找到此选项对话框的地方吗?

更新:问题是我的"调试"菜单没有"例外"项。我自定义菜单以手动添加它。

解决方案

VS2005中有一个"例外"窗口...调试时尝试按Ctrl + Alt + E并单击" Thrown"复选框以获取要停止的例外。

打开解决方案后,转到"调试例外"(Ctrl + D + E)菜单选项。从那里,我们可以选择中断引发或者用户未处理的异常。

编辑:我的实例是用C"配置文件"设置的,也许其他配置文件不存在?

在msdn上查看此内容,它说明了如何进行设置。

本质上,这是步骤(调试过程中):

  • 在"调试"菜单上,单击"例外"。
  • 在"例外"对话框中,为整个类别的例外选择"引发",例如,"公共语言运行时例外"。 -或者-展开该节点,以查找异常类别,例如"公共语言运行时异常",然后为该类别中的特定异常选择"抛出"。

我使用的技术类似于以下内容。根据要调试的内容,定义一个全局变量,该变量可用于一个或者多个try catch块,并使用以下结构:

if(!GlobalTestingBool)
{
   try
   {
      SomeErrorProneMethod();
   }
   catch (...)
   {
      // ... Error handling ...
   }
}
else
{
   SomeErrorProneMethod();
}

我发现这为我提供了更多的测试灵活性,因为仍然有一些我不希望IDE中断的异常。