首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >什么是堆栈痕迹?

什么是堆栈痕迹?
EN

Stack Overflow用户
提问于 2012-03-26 17:53:42
回答 2查看 861关注 0票数 3

可能重复:

CallStack determines where you are going next?

this问题中的评论开始,我以为我知道堆栈跟踪是什么,但我想我不知道。我已经在谷歌上搜索过了,但可以找到一个清晰的答案。

@asawyer says

堆栈跟踪不确定您去过哪里,它告诉您下一步要去哪里。

这里有一个小程序

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Method1();
        }

        private static void Method1()
        {
            Method2();
        }

        private static void Method2()
        {
            Random rnd = new Random();
            int i = rnd.Next(0, 100);

            throw new Exception();

            if (i > 50)
                Method3();
            else
                Method4();
        }

        private static void Method3(){ }

        private static void Method4(){ }
    }
}

这会产生像这样的堆栈跟踪

代码语言:javascript
复制
   at ConsoleApplication1.Program.Method2() in C:\Users\Ash Burlaczenko\Desktop\CSSD\Assignment 3\ConsoleApplication1\ConsoleApplication1\Program.cs:line 25
   at ConsoleApplication1.Program.Method1() in C:\Users\Ash Burlaczenko\Desktop\CSSD\Assignment 3\ConsoleApplication1\ConsoleApplication1\Program.cs:line 17
   at ConsoleApplication1.Program.Main(String[] args) in C:\Users\Ash Burlaczenko\Desktop\CSSD\Assignment 3\ConsoleApplication1\ConsoleApplication1\Program.cs:line 12
   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.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

尽管在发生异常时,代码可以确定将调用哪个方法,因为它知道i的值,但是Stack跟踪没有提到未来的方法调用。

我认为堆栈跟踪告诉您代码在哪里,这一想法有什么问题呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-03-26 18:04:22

堆栈跟踪显示了您曾经去过的地方,并且假设您正常地从当前函数返回,您将最终返回到那里。根据当前函数的内容,可能会首先执行其他内容(即当前函数将调用但尚未执行的函数),它们不会出现在堆栈跟踪中(并且在输入它们之前不能/不会)。

票数 4
EN

Stack Overflow用户

发布于 2012-03-26 19:03:28

这是一个副本。

The call stack does not say "where you came from", but "where you are going next"?

详细情况见我的回答。

还请参阅最近这个有趣的问题,该问题探讨了“等待”延续和“真正堆栈跟踪”延续之间的关系:

C# await vs continuations: not quite the same?

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9877146

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档