堆栈跟踪如何生成? [英] How do stack traces get generated?

查看:118
本文介绍了堆栈跟踪如何生成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

程序中没有一个方法知道堆栈的位置。所有它知道的是它自己的小工作,它做到这一点并返回。那么当抛出一个异常并且打印一个堆栈跟踪时,这是从哪里来的?

No single method in a program "knows" where it is on the stack. All it knows is its own little job, and it does that and returns. So when an Exception is thrown and a stack trace is printed, where does this come from?

是否在JVM中的每个应用程序旁边隐藏着一个单独的线程来监视程序的状态?或者JVM本身是否持有这些信息,异常在某些情况下会从中提取数据?

Is there implicitly a separate Thread running alongside of every application in the JVM that's monitoring the state of the program? Or does the JVM itself hold this information and the Exceptions somehow pull that data from it when they are thrown?

如果是这些情况,是否可以使用一些调用来检索堆栈跟踪(从监视器线程或JVM)不使用抛出异常?

If either of these is the case, is it possible to use some call to retrieve a stack trace (either from the monitor Thread or the JVM) without throwing an Exception?

推荐答案

每个线程都有自己的堆栈。每个方法调用创建一个堆栈帧。如果任何方法的代码发生错误,那将传播给调用方法。这样,JVM可以跟踪哪个方法生成的错误以及调用层次结构。

Every thread will have its own stack. Each method call creates a stack frame. If something wrong happened in code of any method, that will propagated to caller method. This way JVM can trace which method generated error and what is the call hierarchy.

如果您正确观察到堆栈跟踪,您将看到错误发生在顶部的方法底层的层次结构。

If you observe the stack trace properly, you will see the method where error occured at top and the hierarchy in bottom.

youtube 由斯坦福大学教授了解其工作原理。我会建议看它。

There is a great lecture in youtube by a Stanford professor to understand how does it work. I would suggest watching it.

注意:这是理论。如果您想了解API的工作原理,@Peter Lawrey的答案可能会帮助您。

NOTE: This is theory. If you would like to know how API works, @Peter Lawrey answer may help you.

这篇关于堆栈跟踪如何生成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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