有没有可能像点击一个按钮一样轻松地停用/启用整个项目中的所有try catch块?
我需要在调试时使用此功能,当我不想让catch块处理异常时,而是希望VS打断代码,就好像try catch块不存在一样。
目前,我正在注释try / catch块,但这很低效。
环境:使用C#语言的VS 2008。
有没有可能像点击一个按钮一样轻松地停用/启用整个项目中的所有try catch块?
我需要在调试时使用此功能,当我不想让catch块处理异常时,而是希望VS打断代码,就好像try catch块不存在一样。
目前,我正在注释try / catch块,但这很低效。
环境:使用C#语言的VS 2008。
无法停用try/catch块。
但是,为了调试目的,如果您想在特定类型的异常被抛出时立即中断程序,您可以让Visual Studio来实现这一点(Debug -> Exceptions; 选择您感兴趣的异常类型并选中“Thrown”框)。
今天我思考了一下,找到了另一种解决方案。这种方法的优点是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
玩得开心, 问候, 泰德
如果您想在IDE中进行操作,可以通过Debug -> Exceptions对话框来设置当特定/类别/所有异常被抛出时,让IDE停止运行。
您可以更改 Visual Studio 在异常发生时中断的方式。默认情况下,它会在未处理的异常上中断。如果您转到“调试”菜单>“异常”,则可以取消选中“公共语言运行时异常”并在 IDE 处理异常时进行其他更改。例如,您可以让它仅在一种异常上中断;那里有一个很长的列表。
我在尝试调试时偶尔会这样做。