抛出异常时阻止Visual Studio中断

时间:2011-04-12 15:42:11

标签: visual-studio visual-studio-2010 debugging

我测试异常拦截,因此,我不需要Visual Studio在thrown new NullReferenceException("myVar")之类的thinkgs上打破。

我在Debug => Exceptions

下有以下内容

enter image description here

然而,VS打破例外。我该怎么办?

PS。

对于应用程序未处理的异常,我使用Application.UnhandledException“捕获”它们,如下所示:

''' <summary>Occurs when the application encounters an unhandled exception.</summary> '
Private Sub Application_UnhandledException(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledException
  Dim message As String = String.Format("An application UnhandledException were thrown.{1}The application will now terminate.{1}'{0}'{1}{1}StackTrace:{1}{2}", e.Exception.Message, Environment.NewLine, e.Exception.StackTrace)
  MessageBox.Show(message)
End Sub

3 个答案:

答案 0 :(得分:5)

当我开始使用VS2010时,我遇到了同样的问题。我有单元测试,期望异常,我从我的函数中抛出异常。这些异常应该由我的库的用户处理。在Debug-&gt; Exceptions对话框中,我取消选中Common-Runtime Exceptions的User-Unhandled列下的复选框,并且VS停止了这些异常。顺便说一下,我没有在你附加的对话框中看到第二列。

答案 1 :(得分:2)

如果抛出一个未在代码中的任何地方处理的异常,Visual Studio将会中断。它没有任何其他选择:存在未处理的异常。在Visual Studio之外,应用程序将显示错误消息并通知用户发生了未处理的异常。

您在调试中看到的选项 - &gt; “例外”对话框仅允许您配置Visual Studio是否中断所有异常,包括稍后在代码中处理的异常。这些通常被称为“第一次机会”例外。

除此之外,你不应该自己抛出NullReferenceException;这是为运行时框架保留的运行时异常。相反,你应该抛出一个ArgumentNullException

答案 2 :(得分:0)

以下方法适用于Visual Studio 2015(类似的过程可能适用于VS2010)。

取自Visual Studio documentation on managing exceptions with the debugger

  
      
  1. 在“例外设置”窗口中,右键单击窗口,然后选择“显示列”,打开上下文菜单。 (如果您关闭了Just My Code,则不会看到此命令。)
  2.   
  3. 您应该会看到名为“附加操作”的第二列。当用户代码未处理特定异常时,此列显示“继续”,这意味着如果在用户代码中未处理该异常但在外部代码中处理该异常,则调试器不会中断。
  4.   
  5. 您可以针对特定异常更改此设置(选择例外,右键单击,选择/取消选择“在用户代码中未处理时继续”)或整个异常类别(例如,所有公共语言运行时异常) )。
  6.