Selectively ignore thrown exceptions in C # code

I have a function in C # code where a NullReferenceException is thrown periodically (expected behavior) but caught. Is there a way I can tell the Visual Studio debugger not to violate this exception for this particular section of my code?

EDIT I ​​need to break this exception elsewhere in my code, but not in the same function.

+5
source share
3 answers

If I understand correctly and what you are trying to do is debug some NullReferenceException (s), but you want to temporarily ignore others during debugging, you can do this by marking the functions that you want the debugger to ignore DebuggerNonUserCode .

[DebuggerNonUserCode]
private void MyMethod()
{
    // NullReferenceException exceptions caught in this method will
    //  not cause the Debugger to stop here..
}

NOTE that this will only work if exceptions are found in the specified methods. They just won’t make the debugger break if you have a debugger that will always throw exceptions NullReferenceException. And that this only works on methods, and not on arbitrary sections of code inside a method.

+11
source

Assuming the exception does not reach the caller, this can be achieved using DebuggerHiddenAttribute .

From the comments

Visual Studio 2005 , .

    [DebuggerHidden]
    private static void M()
    {
        try
        {
            throw new NullReferenceException();
        }
        catch (Exception)
        {
            //log or do something useful so as not to swallow.
        }            
    }
+1

You can do this, but it affects all exceptions in the solution.

Debug -> Exceptions -> Find... "Null Ref", de-tick Thrown.

-1
source

All Articles