Messing around with the stack trace really doesn't sound like a good idea, even if it is possible (I'm doubtful of that). Tell me, why would you want to do that anyway? The .NET framework itself (the BCL) often uses static utility methods to throw exceptions, in the way that you suggest (ThrowHelper
is its name in at least some parts of the framework), and it certainly does hide anything in the stack trace.
Here's an example stack trace from a test I just ran:
at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
at System.ThrowHelper.ThrowArgumentOutOfRangeException()
at System.Collections.Generic.List`1.get_Item(Int32 index)
at HelloWorld.Program.Main(String[] args) in C:\...\Program.cs:line 23
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
As you can see, the BCL uses the ThrowArgumentOutOfRangeException
method, and it's clearly visible in the stack trace. If you want to mark the helper method with the DebuggerNonUserCode
attribute, then that would seem fair enough to me (though it's not done in the BCL).