Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I prevent the debugger from entering my method when Exception is thrown?

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:

default behavior

and this is my desired behavior:

desired behavior

I have tried adding [DebuggerNonUserCode] and [DebuggerStepThrough] attributes to my Fail() method, but no luck.

like image 844
Mr Anderson Avatar asked Nov 17 '17 08:11

Mr Anderson


People also ask

How do I stop a debugging session?

To end a debugging session in Microsoft Visual Studio, from the Debug menu, choose Stop Debugging.

How do you disable the Debug option Enable just my code?

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.

Does throwing an exception return from a method?

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.

How do I make Visual Studio not stop on exception?

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.


1 Answers

You need to decorate your method with DebuggerHiddenAttribute:

[DebuggerHidden]
public static void Fail() {
    throw new Exception("Fail");
}
like image 85
Evk Avatar answered Oct 14 '22 23:10

Evk