views:

881

answers:

6

Note: The accepted solution doesn't quite work, but it's very close.


Is there a macro or keyboard shortcut that will toggle "break when an exception is thrown" without using the GUI.

Opening the dialog with ctrl+alt+e and checking the "Common Language Runtime Exceptions" "Thrown" box then clicking OK is simple enough, but this is something I do a lot. I would rather have a keyboard shortcut for this.

This question is a duplicate of http://stackoverflow.com/questions/323390/any-have-a-visual-studio-shortcut-macro-for-toggling-break-on-handled-unhandled-e

However, the poster accepted an answer that doesn't really work, and I would really like an answer that does work.

The answer in the duplicate question is not acceptable because it toggles only one specific exception, not the entire CLR group.

"Well write a loop then." you say. But not so fast! Someone tried that already and it was uselessly slow. (Yes I've verified that its slow on my system as well.)

So the challenge is to use a macro to toggle the entire CLR Exceptions category in less than 1 or 2 seconds. This question is a duplicate of http://stackoverflow.com/questions/323390/any-have-a-visual-studio-shortcut-macro-for-toggling-break-on-handled-unhandled-e

+1  A: 

Well, I wrote a VS2008 C# based plug-in that toggles the 386 exceptions, and it takes about 1 second per state toggle. I'm assuming that's due to COM inter-op.

This was based on the VB/macro code in the one of your links. I could not find an easier C++ method (but not ruling it out).

The next level would be to make a plug-in that has a keyboard binding, that then opens the Exceptions UI and then "clicks" the correct tick box for you.

Good luck.

Simeon Pilgrim
Thanks for trying. I suspect the issue is that toggling 1 setting or toggling 386 settings takes about the same amount of time *IF* toggling them as a batch. But I have only found a way to toggle them 1 at a time, so it takes 386 times longer than it should.
dss539
+1 thanks for contributing to this (I didn't want to +1 before because I didn't want this to get auto-accepted as an answer.)
dss539
+7  A: 

Very similar to the other answer, but there is a special ExceptionSetting for the group.

Dim dbg As EnvDTE90.Debugger3 = DTE.Debugger
Dim exSettings As EnvDTE90.ExceptionSettings = dbg.ExceptionGroups.Item("Common Language Runtime Exceptions")
Dim exSetting As EnvDTE90.ExceptionSetting
Try
    exSetting = exSettings.Item("Common Language Runtime Exceptions")
Catch ex As COMException
    If ex.ErrorCode = -2147352565 Then
        exSetting = exSettings.NewException("Common Language Runtime Exceptions", 0)
    End If
End Try

If exSetting.BreakWhenThrown Then
    exSettings.SetBreakWhenThrown(False, exSetting)
Else
    exSettings.SetBreakWhenThrown(True, exSetting)
End If
Bryce Kahle
This gives an "invalid index" error for me with VS 2008. Any ideas?
dss539
Odd. It was working for me perfectly, but now I get the same error you do. Let me do some more digging.
Bryce Kahle
Thanks for looking into it.
dss539
I've updated my code. It should work now. The change was to add the ExceptionSetting if it didn't exist.
Bryce Kahle
Awesome! Thanks!
dss539
Hmm, ok this doesn't toggle the subsections for exceptions though. I just noticed this. :(
dss539
yes, I find the same thing....did anyone ever find a proper solution for this??
miguel
I tried this code, and while it successfully checks the top-level checkbox in the Exceptions dialog, that's the only checkbox that is checked. The checkboxes for individual exceptions remain unchecked, and so the debugger doesn't break when an exception is thrown.
Bradley Grainger
Voted down for same reason given by dss539 and others
Nik
+1  A: 

You could use a tool like AutoHotKey to create a recorded script (mouse clicks or key presses) and then assign it a hotkey that will play it back when pressed...

phalacee
Well part of the reason for wanting this is that the Exceptions dialog requires a non-trivial amount of time to open. My main goal is speed. Reducing repetition is an extra benefit, but not why I'm interested in this. So, good idea, but AHK doesn't quite suit my goals.
dss539
+1  A: 

Just offering some info I found on this (here) as I was scouring the net in my futile attempt to help...

Someone else posed this same question and it was responded to by Gary Chang from MS Support, here's the quoted response:

I am afraid the Macro code cannot manipulate the operations on the Exceptions dialog box...

It's important to note that this posting is from December of 2005 so this response may no longer be accurate; either way, thought I'd throw it out there.

Steve Dignan
Thanks for adding the info. BTW, is it Michael or Gary? On the site you linked, his name is listed as Gary Chang.
dss539
Whoops ... yeah, it's Gary.
Steve Dignan
+1  A: 

The suggestion of setting the special ExceptionSetting for the group does indeed toggle the state of the top-level checkbox. However, it doesn't seem to toggle the individual Exceptions below it in the tree, and moreover, my process does not stop when such exceptions are thrown as it does if I manually check the top-level checkbox. Do you see different behavior?

Ethan
Ethan, yes I also noted this. See my comment on the "accepted answer". My comment is hidden by default. You have to "view all comments".
dss539
A: 

Here's Bryce Kahle's very useful macro blindly updated to run in VS2010:

Sub ToggleExceptions()
    Dim dbg As EnvDTE100.Debugger5 = DTE.Debugger
    Dim exSettings As ExceptionSettings = dbg.ExceptionGroups.Item("Common Language Runtime Exceptions")
    Dim exSetting As ExceptionSetting
    Try
        exSetting = exSettings.Item("Common Language Runtime Exceptions")
    Catch ex As COMException
        If ex.ErrorCode = -2147352565 Then
            exSetting = exSettings.NewException("Common Language Runtime Exceptions", 0)
        End If
    End Try

    If exSetting.BreakWhenThrown Then
        exSettings.SetBreakWhenThrown(False, exSetting)
    Else
        exSettings.SetBreakWhenThrown(True, exSetting)
    End If

End Sub
Peter MacMurchy