例外失去try / catch语句的上下文堆栈跟踪的一部分 [英] Exceptions lose part of stacktrace in try/catch context

查看:215
本文介绍了例外失去try / catch语句的上下文堆栈跟踪的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个例子。在第一种情况下,调试器捕获未处理的异常:

I have two examples. In the first case, debugger catches the unhandled exception:

static void Main(string[] args) {
    Exec();
}
static void Exec() {
    throw new Exception();
}

和异常具有完全的堆栈跟踪:

And the exception has full stacktrace:

   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 the breakpoint the exception has short stacktrace:

   at ConsoleApplication28.Program.Exec()

为什么踪迹被切割到含有法在第二种情况下,以及如何预防?我需要一些bug报告全堆栈跟踪,否则就不是有时可能发现有问题,没有完整的堆栈跟踪。

Why stacktraces are cut to the containing method in the second case, and how to prevent it? I need full stacktrace for bugreports, otherwise it's not possible sometimes to find there the problem is, without full stacktrace.

推荐答案

什么你'再在Visual Studio调试器看到的是未处理的异常是在Visual Studio宿主进程被俘获(即所有前两个栈帧之后的那部分VS主持人管道)。如果禁用托管进程(项目属性 - >启用的Visual Studio宿主进程),你会看到在两种情况下一个短堆栈跟踪(即使你不会看到主在第二种情况下,因为异常处理,不许传播到)。这较短的堆栈跟踪是堆栈跟踪你会看到如果你正在运行调试器之外的应用程序

What you're seeing in the Visual Studio debugger is the unhandled exception that the Visual Studio Hosting Process is trapping (ie everything after the first two stack frames is part of that VS "host plumbing"). If you disable the hosting process (Project Properties->Enable Visual Studio hosting process), you'll see a "short" stack trace in both scenarios (notwithstanding that you won't see the stack frame for Main in your second case because the exception is "handled", not allowed to propagate up to Main). This shorter stack trace is the stack trace you would see if you were running the application outside the debugger.

该堆栈的工作,你会想象 - 每个方法调用推另一个堆栈帧到它,并且在该方法的结束它的栈帧是弹出,或从堆栈中移除。除了你看到的堆栈跟踪由从框架在异常被抛出,回哪里异常最终处理的帧栈帧,因为堆栈展开。

The stack works as you would imagine - each method call pushes another stack frame onto it, and at the end of the method its stack frame is "popped", or removed from the stack. The stack trace you see on the exception is composed of the stack frames from the frame where the exception was thrown, back to the frame where the exception is ultimately handled, as the stack is "unwound".

这篇关于例外失去try / catch语句的上下文堆栈跟踪的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆