如何暂时停用所有try / catch块?

时间:2008-12-07 21:37:50

标签: visual-studio

是否有可能在整个项目中停用/激活所有try catch块,就像点击按钮一样简单?

当我不希望catch块处理异常时,我需要这个用于调试,而是宁愿VS中断代码,好像try catch块不存在一样。

目前我正在评论try / catch块,但效率很低。

环境:VS 2008以C#为语言。

5 个答案:

答案 0 :(得分:33)

在抛出异常时捕获异常(Win32用语中的“第一次机会异常”):

    VS2008中的
  • :转到调试例外......

  • VS2015的
  • :已移至调试> Windows>例外设置

然后选中投掷公共语言运行时例外

enter image description here

答案 1 :(得分:14)

不,没有。为什么会这样?你想要实现什么目标?

如果要在抛出特定类型的异常后立即中断,可以让Visual Studio执行此操作(Debug - > Exceptions;选择您感兴趣的异常类型并选中“Thrown”框)。我发现很难想到你为什么要关闭错误处理......

编辑:我以前没想过的一点 - 您使用的是哪个版本的Visual Studio?如果它是Express,我不确定这是否支持 - 我可以检查,但它需要一段时间。如果我需要,请告诉我......

答案 2 :(得分:1)

如果你想在IDE中做,Debug - >例外是一个对话框,您可以在抛出特定/类别/所有异常时让IDE中断。

答案 3 :(得分:1)

今天想到这个并找到了另一个解决方案。这样做的好处是IDE将在发生异常的确切位置停止。

全球定义的地方:

Namespace Global.System
  Public Class NeverOccurException
    Inherits Exception
  End Class
End Namespace

在每个源代码文件的开头:

#If DEBUG
  Imports CatchAtReleaseException = System.NeverOccurException
#Else
  Imports CatchAtReleaseException = System.Exception
#End If

用法:

  'Compiled in DEBUG-Mode the TryCatch is "disabled", because the
  'the ALIAS CatchAtReleaseException is set to "NeverOccurException"

  'Compiled as RELEASE the TryCatch is "enabled", because the ALIAS
  'is set to the regular "System.Exception"

  Public Sub SampleSub()
    Try
      '...
    Catch ex As CatchAtReleaseException
    End Try
  End Sub

玩得开心, 问候, 泰德

答案 4 :(得分:0)

您可以更改发生异常时Visual Studio中断的方式。默认情况下,它会中断未处理的异常。如果你去菜单Debug>例外情况,您可以取消选中公共语言运行时异常,并在发生异常时对IDE的行为进行其他更改。例如,你可以让它只打破一种异常;那里有很长的名单。

我在尝试调试的极少数情况下完成了这项工作。

相关问题