|
It turns out that, in practice, code like that shown above is often executed in the context of a WinForms application where the LastName property is bound to some control via .NET databinding. In this case, .NET databinding is built with the expectation that errors such as this may be thrown inside of a property setter and it that the databinding infrastructure will handle the exception. This means that we really do not want the debugger to break on the exception. We want the .NET databinding apparatus to handle this exception so that we in turn can use the databinding mechanisms to provide an error bullet or some other UI manifestation of the error.
So why is any of this important to non-framework developers?
The reason is that the case described above is not limited to third-party framework code. In fact we often find within large applications the need to have lower levels in the application stack throw errors that we "expect" to catch and handle further up the stack. Each of these is a candidate for the DebuggerNonUserCodeAttribute.
So what if we want the best of both worlds? What if we sometimes want to debug this at the lowest possible level and sometimes we want to debug at a higher "intent" level?
Visual Studio provides a toggle to do exactly this. Inside of the Tools - Options - Debugging window there is a checkbox next to the option "Enable Just My Code". By default, this is checked and the debugger will behave as described above. However, by unchecking this option, we in effect remove the effect of the DebuggerNonUserCodeAttribute and revert back to our original behavior.
There is also a DebuggerHiddenAttribute which hides the code from the debugger, even if "Enable Just My Code" is turned off. This is intended for those cases where we never want to let the developer debug into the specified method or property at all. In effect, this constitutes a more absolute version of the DebuggerNonUserCodeAttribute. |