gpt4 book ai didi

c# - 什么是堆栈跟踪?

转载 作者:行者123 更新时间:2023-12-03 03:02:37 25 4
gpt4 key购买 nike

Possible Duplicate:
CallStack determines where you are going next?

this中的评论开始问题,我以为我知道堆栈跟踪是什么,但我想我不知道。我用 Google 搜索过,但找到了明确的答案。

@asawyer says

The stacktrace doesn't identify where you've been, it tells you where you are going next.

这是一个小程序

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 的值。堆栈跟踪没有提及 future 的方法调用。

堆栈跟踪告诉你代码在哪里的想法哪里错了?

最佳答案

堆栈跟踪显示您去过的位置,并假设您从当前函数正常返回,您最终将返回到哪里。根据当前函数的内容,可能还有其他事情将首先执行(即当前函数将调用但尚未调用的函数),这些事情不会显示在堆栈跟踪中(并且不能/在您输入它们之前不会)。

关于c# - 什么是堆栈跟踪?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9877146/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com