Selectively ignore thrown exceptions in C# code

前端 未结 3 1446
时光取名叫无心
时光取名叫无心 2021-01-06 01:32

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 to n

3条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-06 01:53

    Assuming the exception does not bubble up to the caller, this can be achieved with DebuggerHiddenAttribute.

    From the remarks

    the Visual Studio 2005 debugger does not stop in a method marked with this attribute and does not allow a breakpoint to be set in the method.

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

提交回复
热议问题