Ale*_*nko 5 .net c# exception-handling exception stack-trace
我有两个例子.在第一种情况下,调试器捕获未处理的异常:
static void Main(string[] args) {
Exec();
}
static void Exec() {
throw new Exception();
}
Run Code Online (Sandbox Code Playgroud)
并且异常具有完整的堆栈跟踪:
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()
Run Code Online (Sandbox Code Playgroud)
第二种情况:
static void Main(string[] args) {
Exec();
}
static void Exec() {
try {
throw new Exception();
}
catch (Exception ex) {
} // Breakpoint
}
Run Code Online (Sandbox Code Playgroud)
在断点处,异常具有短栈跟踪:
at ConsoleApplication28.Program.Exec()
Run Code Online (Sandbox Code Playgroud)
为什么在第二种情况下,stacktraces被切割为包含方法,以及如何防止它?我需要完整的堆栈跟踪来进行bug报告,否则有时候找不到问题是不可能的,没有完整的堆栈跟踪.
您在 Visual Studio 调试器中看到的是 Visual Studio 托管进程正在捕获的未处理的异常(即前两个堆栈帧之后的所有内容都是 VS“主机管道”的一部分)。如果禁用托管进程(项目属性->启用 Visual Studio 托管进程),您将在两种情况下看到“短”堆栈跟踪(尽管在第二种情况下您不会看到堆栈帧,因为Main异常已“处理”,不允许传播到Main)。这个较短的堆栈跟踪是您在调试器外部运行应用程序时看到的堆栈跟踪。
堆栈的工作方式正如您所想象的那样 - 每个方法调用都会将另一个堆栈帧推入其中,并且在方法结束时,其堆栈帧被“弹出”,或从堆栈中删除。您在异常上看到的堆栈跟踪由从抛出异常的帧到最终处理异常的帧(因为堆栈“展开”)的堆栈帧组成。