Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

  • I know I don't have to have the blanket catch on "all clr exceptions", however the list of possible exceptions is about 2 miles long and it takes forever to scroll through and pick the ones you want (and then what if I miss some).
like image 715
Orion Edwards Avatar asked May 06 '09 23:05

Orion Edwards


People also ask

How do you break an exception thrown?

Tell the debugger to break when an exception is thrownIn the Exception Settings window (Debug > Windows > Exception Settings), expand the node for a category of exceptions, such as Common Language Runtime Exceptions. Then select the check box for a specific exception within that category, such as System.

How do I change exceptions in Visual Studio?

You can open the exception settings window by navigating from Debug -> Windows -> Exception Settings. With Visual Studio 2017, it has some additional features and which is quite useful. You can now set the condition on the exception in the Exception settings Windows.

Where is the unhandled exception in Visual Studio?

Go to the Debug menu, Exceptions. Now run your code under the debugger and vs will break whenever an exception is thrown. Now open the call stack (Debug > Window > Call Stack) to see what functions are causing the problem.


2 Answers

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.

like image 35
IV. Avatar answered Oct 19 '22 08:10

IV.


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
like image 181
Michael Lehenbauer Avatar answered Oct 19 '22 07:10

Michael Lehenbauer