我的宏在运行时忽略当前的 CLR 异常。当在调试时弹出异常时,它就像一个按钮“禁用捕获此异常类型”。
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a
Imports EnvDTE100
Imports System.Diagnostics
Imports Microsoft.VisualBasic
Imports Microsoft.VisualBasic.ControlChars
' execute Macros.MyMacros.VSDebuggerExceptions.IgnoreCurrentExceptionWhenThrown from VS Command Window
Public Module VSDebuggerExceptions
Sub BreakWhenThrown(Optional ByVal strException As String = "")
Dim dbg As Debugger3 = DTE.Debugger
Dim eg As ExceptionSettings = _
dbg.ExceptionGroups.Item("Common Language Runtime Exceptions")
eg.SetBreakWhenThrown(True, eg.Item(strException))
End Sub
' copied from Utilities module (samples)
Function GetOutputWindowPane(ByVal Name As String, Optional ByVal show As Boolean = True) As OutputWindowPane
Dim window As Window
Dim outputWindow As OutputWindow
Dim outputWindowPane As OutputWindowPane
window = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput)
If show Then window.Visible = True
outputWindow = window.Object
Try
outputWindowPane = outputWindow.OutputWindowPanes.Item(Name)
Catch e As System.Exception
outputWindowPane = outputWindow.OutputWindowPanes.Add(Name)
End Try
outputWindowPane.Activate()
Return outputWindowPane
End Function
Private WithEvents t As Timers.Timer
' Adds the current exception to ignore list
Sub IgnoreCurrentExceptionWhenThrown()
Dim commandWin As EnvDTE.CommandWindow
commandWin = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindCommandWindow).Object
Select Case DTE.Debugger.CurrentMode
Case dbgDebugMode.dbgDesignMode
commandWin.OutputString("This macro is not enabled in Design Mode. Run it in Break Mode." + vbCrLf)
Return
Case dbgDebugMode.dbgRunMode
commandWin.OutputString("This macro is not enabled in Run Mode. Run it in Break Mode." + vbCrLf)
Return
End Select
commandWin.OutputString(Environment.NewLine)
commandWin.OutputString("Trying to get the information about current exception.." + Environment.NewLine)
Dim dbg As Debugger3 = DTE.Debugger
Dim currentExpression As Expression = dbg.GetExpression("$exception", False)
Try
Dim currentExceptionTypeString As String = currentExpression.DataMembers.Item(1).Type
commandWin.OutputString("Detected current exception type is : " + currentExceptionTypeString + Environment.NewLine)
Dim flag As Boolean = True
Dim eg As ExceptionSettings = dbg.ExceptionGroups.Item("Common Language Runtime Exceptions")
Try
eg.SetBreakWhenThrown(False, eg.Item(currentExceptionTypeString))
Catch exc As Exception
commandWin.OutputString("Cannot find this exception, trying to create.." + currentExceptionTypeString + Environment.NewLine)
'
eg.NewException(currentExceptionTypeString, New Random().Next)
eg.SetBreakWhenThrown(False, eg.Item(currentExceptionTypeString))
eg.SetBreakWhenUserUnhandled(True, eg.Item(currentExceptionTypeString))
flag = False
End Try
commandWin.OutputString(Environment.NewLine)
commandWin.OutputString("Exception '" + currentExceptionTypeString + "' added to ignore list.")
commandWin.OutputString(Environment.NewLine)
t = New Timers.Timer()
' small interval to send keys after DTE will start to exec command
t.Interval = 0.1
t.Start()
DTE.ExecuteCommand("Debug.Exceptions")
Catch exc As Exception
commandWin.OutputString("Error occured")
End Try
End Sub
Private Sub t_Elapsed(ByVal ee As Object, ByVal dd As Timers.ElapsedEventArgs) Handles t.Elapsed
t.Stop()
' only press Ok to apply changed exceptions settings to debugger
System.Windows.Forms.SendKeys.SendWait("%t")
System.Windows.Forms.SendKeys.SendWait("{ENTER}")
End Sub
End Module