我希望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(在Debug菜单上没有例外选项)。

有人知道在Visual Studio中“当抛出异常时”组框的哪里找到这个选项对话框,其中有“进入调试器”的选项吗?

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


当前回答

VS2005中有一个“例外”窗口…调试时尝试按Ctrl+Alt+E,并单击要停止的异常的“抛出”复选框。

其他回答

从Visual Studio 2015及以后,你需要进入“异常设置”对话框(Ctrl+Alt+E)并选中“公共语言运行时异常”(或你想要的特定的一个,例如ArgumentNullException),使其在处理异常时中断。

步骤1 步骤2

VS2005中有一个“例外”窗口…调试时尝试按Ctrl+Alt+E,并单击要停止的异常的“抛出”复选框。

我花了一段时间才找到期望设置的新地方,因此有了一个新的答案。

自Visual Studio 2015以来,您可以在异常设置窗口(调试->Windows->异常设置)中控制要停止的异常。快捷键仍然是Ctrl-Alt-E。

处理自定义异常的最简单方法是选择“不在此列表中的所有异常”。

以下是英文版的截图:

以下是德国版的截图:

检查使用调试器管理异常页面,它解释了如何设置它。

本质上,以下是(在调试期间)的步骤:

在“调试”菜单上,单击“例外”。 在“异常”对话框中,为整个异常类别选择“抛出”,例如“公共语言运行时异常”。 或者, 展开异常类别(例如,公共语言运行时异常)的节点,并为该类别中的特定异常选择抛出。

The online documentation seems a little unclear, so I just performed a little test. Choosing to break on Thrown from the Exceptions dialog box causes the program execution to break on any exception, handled or unhandled. If you want to break on handled exceptions only, it seems your only recourse is to go through your code and put breakpoints on all your handled exceptions. This seems a little excessive, so it might be better to add a debug statement whenever you handle an exception. Then when you see that output, you can set a breakpoint at that line in the code.