Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop a custom ThrowHelper from appearing in your StackTrace?

Tags:

c#

.net

A pattern you'll find in the .net base class library is that of the ThrowHelper. In essence it cuts down the amount of byte code per method.

Anyway, I was wondering if there was an attribute directive to stop an error caused in the throw Helper from stopping inside the helper. I'd prefer the debugger to stop at the calling line.

i.e.

  ThrowHelper.ThrowAnException ()

rather than inside ThrowAnException()

like image 393
sgtz Avatar asked Jan 15 '23 07:01

sgtz


2 Answers

You can also mark your throwing method with an attribute:

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

Then debugger will not enter this method.

like image 176
Rafal Avatar answered Jan 29 '23 11:01

Rafal


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

throw ThrowHelper.CreateAnException();
like image 37
David Yaw Avatar answered Jan 29 '23 10:01

David Yaw