JVM如何知道在运行时捕获异常的位置? [英] How does the JVM know where to catch an exception at runtime?

查看:366
本文介绍了JVM如何知道在运行时捕获异常的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我的理解, throw 是一个主要的jvm命令。当这个被调用时,JVM检查当前调用堆栈是否可以捕获它。如果它不能,那么java只是简单地弹出调用栈,就好像一个return被调用一样。那么jvm检查当前调用堆栈是否可以捕获它等等递归地。

From my understanding, throw is a primative jvm command. When this is called, the JVM "checks if the current call stack can catch it". if it can't, then java simply pops the call stack almost exactly as if a return was called. then the jvm "checks if the current call stack can catch it" and so on recursively.

我的问题:JVM如何在算法上可以知道在哪里调用堆栈可以捕获给定的异常?每个调用堆栈条目中是否存有元数据,将异常映射到代码块?堆中有静态数据结构吗?因为某处必须有数据跟踪。

My question: how is it algorithmically possible for the JVM to know where in the call stack can catch a given exception? Is there metadata stored in each call stack entry mapping exceptions to code blocks? is there a static data structure in the heap that somehow keeps track of this? because somewhere there has to be data keeping track of that.

推荐答案

JVM规范有关于此的详细信息。

The JVM specification has details about this.

具体来说,第4.7.3节提供了有关异常表的详细信息,这是一系列条目,说明哪些异常被捕获在哪个指令之间。 第3.12节给出了具体的这个例子就是这样的。

In particular, section 4.7.3 gives details about the exception table, which is a series of entries saying which exceptions are caught between which instructions. Section 3.12 gives a concrete example of this.

这个元数据如何映射到JIT的本地代码当然是和实现相关的。例如,可以从原始JITted代码中的每个指令位置返回到原始字节码位置,此时可以查询异常表以查找正确的处理程序。

How this metadata is mapped into native code for the JIT is a different matter, of course - and implementation-specific. For example, there could be some mapping back from each instruction location in the native JITted code back to the original bytecode location, at which point the exception table can be consulted to find the right handler.

这篇关于JVM如何知道在运行时捕获异常的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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