How to stop custom ThrowHelper from your StackTrace?

The sample you will find in the .net base class library is the ThrowHelper template. In essence, it reduces the number of byte codes for each method.

Anyway, I was wondering if an attribute directive exists to stop the error caused by calling throw helper inside the helper. I would prefer the debugger to stop at the calling line.

i.e.

ThrowHelper.ThrowAnException () 

not inside ThrowAnException ()

+4
source share
2 answers

You can also mark your attribute throwing method:

 class ThrowHelper { [DebuggerStepThrough] public static void Throw() { throw new InvalidOperationException(); } } 

Then the debugger will not introduce this method.

+5
source

You could make a helper method really not an exception, just create it. The stack trace will point to the throw statement in your real method, and not to anything in ThrowHelper.

 throw ThrowHelper.CreateAnException(); 
+4
source

Source: https://habr.com/ru/post/1440703/


All Articles