可能重复:
CallStack determines where you are going next?
从this问题中的评论开始,我以为我知道堆栈跟踪是什么,但我想我不知道。我已经在谷歌上搜索过了,但可以找到一个清晰的答案。
堆栈跟踪不确定您去过哪里,它告诉您下一步要去哪里。
这里有一个小程序
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(){ }
}
}这会产生像这样的堆栈跟踪
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跟踪没有提到未来的方法调用。
我认为堆栈跟踪告诉您代码在哪里,这一想法有什么问题呢?
发布于 2012-03-26 18:04:22
堆栈跟踪显示了您曾经去过的地方,并且假设您正常地从当前函数返回,您将最终返回到那里。根据当前函数的内容,可能会首先执行其他内容(即当前函数将调用但尚未执行的函数),它们不会出现在堆栈跟踪中(并且在输入它们之前不能/不会)。
发布于 2012-03-26 19:03:28
这是一个副本。
The call stack does not say "where you came from", but "where you are going next"?
详细情况见我的回答。
还请参阅最近这个有趣的问题,该问题探讨了“等待”延续和“真正堆栈跟踪”延续之间的关系:
https://stackoverflow.com/questions/9877146
复制相似问题