【发布时间】:2014-08-09 11:00:20
【问题描述】:
我有两个例子。在第一种情况下,调试器捕获未处理的异常:
static void Main(string[] args) {
Exec();
}
static void Exec() {
throw new Exception();
}
并且异常具有完整的堆栈跟踪:
at ConsoleApplication28.Program.Exec()
at ConsoleApplication28.Program.Main(String[] args)
at System.AppDomain._nExecuteAssembly(RuntimeAssembly 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.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
第二种情况:
static void Main(string[] args) {
Exec();
}
static void Exec() {
try {
throw new Exception();
}
catch (Exception ex) {
} // Breakpoint
}
在断点处,异常的堆栈跟踪很短:
at ConsoleApplication28.Program.Exec()
为什么在第二种情况下stacktraces被切到包含方法,以及如何防止它?我需要完整的堆栈跟踪来获取错误报告,否则有时无法在没有完整堆栈跟踪的情况下找到问题所在。
【问题讨论】:
-
“我需要完整的堆栈跟踪来获取错误报告,否则有时在没有完整堆栈跟踪的情况下无法找到问题所在。”要回答您问题的这一部分,您需要在 Main() 方法中可能发生异常的“上方”某处插入一个 try-catch 结构,可能就在顶部。在 catch 子句中,您应该将 exceptionObject.ToString() 写入某个文件或其他内容以供以后分析。
标签: c# .net exception exception-handling stack-trace