Jos*_*gle 9 .net language-agnostic exception
是否可以使用自定义堆栈跟踪抛出异常(可能是任何异常)?
作为一个具体的例子:假设我有一组可能引发异常的小型静态实用程序方法.但是,我希望异常看起来源自前一个方法而不是实用方法(我想忽略跟踪的第一帧).
搞乱堆栈跟踪听起来确实不是一个好主意,即使有可能(我对此表示怀疑)。告诉我,你为什么要这么做?.NET 框架本身(BCL)经常使用静态实用程序方法来抛出异常,按照您建议的方式(ThrowHelper至少在框架的某些部分是它的名称),并且它确实隐藏了堆栈跟踪中的任何内容。
这是我刚刚运行的测试的堆栈跟踪示例:
在 System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument 参数,ExceptionResource 资源) 在 System.ThrowHelper.ThrowArgumentOutOfRangeException() 在 System.Collections.Generic.List`1.get_Item(Int32 索引) 在 C:\...\Program.cs 中的 HelloWorld.Program.Main(String[] args):第 23 行 在 System.AppDomain._nExecuteAssembly(程序集,String[] args) 在 System.AppDomain.ExecuteAssembly(字符串 assemblyFile,证据 assemblySecurity,字符串 [] args) 在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 在 System.Threading.ThreadHelper.ThreadStart_Context(对象状态) 在 System.Threading.ExecutionContext.Run(ExecutionContextexecutionContext,ContextCallback 回调,对象状态) 在 System.Threading.ThreadHelper.ThreadStart()
正如您所看到的,BCL 使用该ThrowArgumentOutOfRangeException方法,并且它在堆栈跟踪中清晰可见。如果您想用属性标记辅助方法DebuggerNonUserCode,那么这对我来说似乎足够公平(尽管 BCL 中没有这样做)。
| 归档时间: |
|
| 查看次数: |
4184 次 |
| 最近记录: |