How can I prevent the debugger from entering my method when an exception is thrown, instead show the exception at the method's call site?
For example, if one's code causes an exception to be thrown from mscorlib, the debugger (obviously) does not take them into the non-user code to show the exception's source, only shows the exception at the call site.
In other words, this the default behavior:
and this is my desired behavior:
I have tried adding [DebuggerNonUserCode]
and [DebuggerStepThrough]
attributes to my Fail()
method, but no luck.
To end a debugging session in Microsoft Visual Studio, from the Debug menu, choose Stop Debugging.
To enable or disable Just My Code in Visual Studio, under Tools > Options (or Debug > Options) > Debugging > General, select or deselect Enable Just My Code.
Yes, throwing an exception causes the method to return immediately. However, it's not a normal return. For example, there is no return value. The only way to capture control from a method that is returning as a result of an exception being thrown, is to have an exception handler.
Note: you can uncheck Break when this exception type is thrown directly in the exception handler and continue debugging (press F5 ). Visual Studio will add this exception type to the Exception settings and will remember that it shouldn't break on this exception again.
You need to decorate your method with DebuggerHiddenAttribute:
[DebuggerHidden]
public static void Fail() {
throw new Exception("Fail");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With