Orion Edwards
Orion Edwards

Reputation: 123622

Can I adjust the visual studio "Break when an exception is thrown" options programmatically?

Briefly:

In Visual Studio 2008, the Debug menu has an Exceptions... option.
When clicking this, it brings up the "Break when an exception is thrown" dialog, wherein I tick the box next to "Common Language Runtime Exceptions".

I want to be able to tick / untick this box programmatically.

Elaboration:

This causes the debugger to break when any CLR exception is thrown (not when it's caught and re-thrown), so this is great for troubleshooting.

The problem is, it catches all CLR exceptions, and the .NET framework happens to throw and catch a bunch of exceptions internally, which also get caught. This causes the debugger to break on a bunch of exceptions which I really don't care about as they are internal to the framework and not a problem.
WCF is particularly bad at this, and as fortune has it, my app uses WCF all over the place.

What I'd like to do, is have the checkbox turned off, and once my app has started up (and got past the WCF connection phase and all the internal exceptions), then have it turned on, to break on all exceptions from now on.

Upvotes: 20

Views: 7479

Answers (6)

Michael Lehenbauer
Michael Lehenbauer

Reputation: 16309

You can turn them on/off through Visual Studio's automation API (called DTE). Take a look at the Debugger3.ExceptionGroups API. For example:

' Turn off NullArgumentException.
Dim debugger As Debugger3 = DTE.Debugger
Dim exceptionGroup As ExceptionSettings = dbg.ExceptionGroups.Item("Common Language Runtime Exceptions")
exceptionGroup.SetBreakWhenThrown(False, "System.NullArgumentException")

So you could easily write a macro to turn specific exceptions on / off. To invoke the macro from your application you can launch "devenv /command".

Alternatively, you can use DTE from out-of proc and automate Visual Studio directly (no macros involved).

More info:

  1. Customizing exception handling in the VS Debugger
  2. Debugger3.ExceptionGroups
  3. devenv /command
  4. How to: Get References to the DTE and DTE2 Objects

Upvotes: 30

Protiguous
Protiguous

Reputation: 103

There's also

if ( Debugger.IsAttached ) { Debugger.Break() }

Upvotes: 0

IV.
IV.

Reputation: 1233

This doesn't address your question directly, but there's a handy chord ctrl-D + E that brings up the debugging exceptions dialog. Ctrl+Alt+E will do the same thing.

Upvotes: 8

Jimmy
Jimmy

Reputation: 28376

You can probably do this by creating a macro (as shown in Michael Lehenbauer's example), and then re-configure your breakpoint to run the macro and continue execution. You could do the same at the end of the code section you care about to disable the option again, or manually disable it between debugging sessions.

Upvotes: 1

Christopher Edwards
Christopher Edwards

Reputation: 6659

Off the top of my head (so it may be nonsense) you could set the debugger to break only on unhandled CLR exceptons, then create a handler delegate/event for all exceptions during the init code and swallow the CLR ones (OK, OK, only in debug mode) and then remove the delegate/event at the point in the code where you want VS to start breaking on CLR exceptions.

Upvotes: 1

JP Alioto
JP Alioto

Reputation: 45117

Sure, you can do it with a Visual Studio add-in or macro.

Upvotes: 0

Related Questions