Toggle “Break when an exception is thrown.” using macro or keyboard shortcut

前端 未结 10 1525
情深已故
情深已故 2020-11-29 23:51

Edit: Visual Studio 2015\'s new exception window is so much faster than the old dialog that I no longer care as much about using a keyboard shortcut for i

10条回答
  •  被撕碎了的回忆
    2020-11-30 00:21

    My macro to ignore current CLR exception in runtime. It works like a button 'disable catching this exception type' when an exception pops at debug-time.

    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
    

提交回复
热议问题