Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I enable/disable breaking on Exceptions programmatically?

I want to be able to break on Exceptions when debugging... like in Visual Studio 2008's Menu Debug/Exception Dialog, except my program has many valid exceptions before I get to the bit I wish to debug.

So instead of manually enabling and disabling it using the dialog every time is it possible to do it automatically with a #pragma or some other method so it only happens in a specific piece of code?

like image 587
AnthonyLambert Avatar asked Apr 29 '10 14:04

AnthonyLambert


1 Answers

The only way to do something close to this is by putting the DebuggerNonUserCodeAttribute on your method.

This will ensure any exceptions in the marked method will not cause a break on exception.

Good explanation of it here...

This is an attribute that you put against a method to tell the debugger "Nothing to do with me guv'. Ain't my code!". The gullible debugger will believe you, and won't break in that method: using the attribute makes the debugger skip the method altogether, even when you're stepping through code; exceptions that occur, and are then caught within the method won't break into the debugger. It will treat it as if it were a call to a Framework assembly, and should an exception go unhandled, it will be reported one level up the call stack, in the code that called the method.

Code example:

public class Foo
{
    [DebuggerNonUserCode]
    public void MethodThatThrowsException()
    {
        ...
    {
}
like image 131
KrisG Avatar answered Sep 30 '22 14:09

KrisG