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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/116896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 09:38:43  来源:igfitidea点击:

Visual Studio: How to break on handled exceptions?

visual-studioexceptiondebugging

提问by Ian Boyd

I would like Visual Studio to break when a handled exception happens (i.e. I don't just want to see a "First chance" message, I want to debug the actual exception).

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

e.g. I want the debugger to break at the exception:

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

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

I came across thesenotes for Visual Studio.NET:

我遇到了 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.

1) 在 VS.NET 中,转到调试菜单>>“异常...”>>“公共语言运行时异常”>>“系统”并选择“System.NullReferenceException”

2)在该对话框的底部有一个“抛出异常时:”组框,选择“进入调试器”

3)运行你的场景。抛出异常时,调试器将停止并通过一个对话框通知您,内容类似于:“已抛出类型为“System.NullReferenceException”的异常。[中断] [继续]”

点击[中断]。这会将您置于导致问题的代码行上。

But they do not apply to Visual Studio 2005 (there is no Exceptionsoption on the Debugmenu).

但它们不适用于 Visual Studio 2005(调试菜单上没有异常选项)。

Does anyone know where the find this options dialog in Visual Studio that the "When the exception is thrown" group box, with the option to "Break into the debugger"?

有谁知道在 Visual Studio 中哪里可以找到“抛出异常时”组框的此选项对话框,并带有“中断调试器”选项?

Update: The problem was that my Debugmenu didn't have an Exceptionsitem. I customized the menu to manually add it.

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

回答by Austin Salonen

With a solution open, go to the Debug - Exceptions (Ctrl+D,E) menu option. From there you can choose to break on Thrownor User-unhandledexceptions.

打开解决方案后,转到 Debug - Exceptions ( Ctrl+ D, E) 菜单选项。从那里您可以选择中断抛出用户未处理的异常。

EDIT: My instance is set up with the C# "profile" perhaps it isn't there for other profiles?

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

回答by Rob Walker

There is an 'exceptions' window in VS2005 ... try Ctrl+Alt+Ewhen debugging and click on the 'Thrown' checkbox for the exception you want to stop on.

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

回答by Beginner

Took me a while to find the new place for expection settings, therefore a new answer.

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

Since Visual Studio 2015you control which Exceptions to stop on in the Exception Settings Window (Debug->Windows->Exception Settings). The shortcut is still Ctrl-Alt-E.

从 Visual Studio 2015 开始,您可以在异常设置窗口(调试->Windows->异常设置)中控制要停止的异常。捷径还是Ctrl- Alt- E

The simplest way to handle custom exceptions is selecting "all exceptions not in this list".

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

Here is a screenshot from the english version:

这是英文版的截图:

enter image description here

在此处输入图片说明

Here is a screenshot from the german version:

这是德文版的截图:

enter image description here

在此处输入图片说明

回答by Sameer Alibhai

From Visual Studio 2015 and onward, you need to go to the "Exception Settings" dialog (Ctrl+Alt+E) and check off the "Common Language Runtime Exceptions" (or a specific one you want i.e. ArgumentNullException) to make it break on handledexceptions.

从 Visual Studio 2015 开始,您需要转到“异常设置”对话框 ( Ctrl+ Alt+ E) 并选中“公共语言运行时异常”(或您想要的特定异常ArgumentNullException)以使其在处理异常时中断。

Step 1 Step 1Step 2 Step 2

步骤 1 步骤1步骤 2 第2步

回答by Guy Starbuck

Check Managing Exceptions with the Debuggerpage, it explains how to set this up.

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

Essentially, here are the steps (during debugging):

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

  1. On the Debug menu, click Exceptions.

  2. In the Exceptions dialog box, select Thrown for an entire category of exceptions, for example, Common Language Runtime Exceptions.

    -or-

    Expand the node for a category of exceptions, for example, Common Language Runtime Exceptions, and select Thrown for a specific exception within that category.

  1. 在调试菜单上,单击异常。

  2. 在“异常”对话框中,为整个异常类别选择“抛出”,例如,公共语言运行时异常。

    -或者-

    展开异常类别的节点,例如公共语言运行时异常,然后为该类别中的特定异常选择抛出。

回答by Spencer Ruport

A technique I use is something like the following. Define a global variable that you can use for one or multiple try catch blocks depending on what you're trying to debug and use the following structure:

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

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

I find this gives me a bit more flexibility in terms of testing because there are still some exceptions I don't want the IDE to break on.

我发现这在测试方面给了我更多的灵活性,因为仍然有一些我不希望 IDE 中断的例外情况。

回答by markysdad

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 anyexception, 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.

网上的文档好像有点不清楚,所以就做了个小测试。从异常对话框中选择在抛出时中断会导致程序执行在任何已处理或未处理的异常时中断。如果您只想中断已处理的异常,似乎您唯一的办法就是检查您的代码并在所有已处理的异常上放置断点。这似乎有点过分,因此在处理异常时添加调试语句可能会更好。然后,当您看到该输出时,您可以在代码中的该行设置断点。