问题描述
它是否只是在每次完成除法时检查除数是否与零不同(即使在JIT编码中)?
Does it simply check if divisor is different from zero every time there is division done (even in JIT-ed code)?
我的意思是VM如何设法抛出以前没有被操作系统杀死的异常?
I mean how VM manages to throw an exception without being previously killed by the OS?
推荐答案
在Unix环境中,除零是 signal
通过 SIGFPE
引导,JVM将安装一个信号处理程序,用于捕获 SIGFPE
反过来抛出
s ArithmeticException
。如果您对内部感兴趣,请参阅例如男子信号
In an Unix environment, in which division-by-zero is signal
led via SIGFPE
, the JVM will have installed a signal handler which traps the SIGFPE
and in turn throw
s an ArithmeticException
. If you're interested in the internals, see e.g. man signal
我认为OP所要求的是基于以下事实:直到/除非 SIGFPE
处理程序到位,大多数进程将在接收此信号时采取默认操作,即终止。因此,例如,一个C程序
What I believe the OP is asking is based on the fact that, until/unless a SIGFPE
handler is in place, most processes will take the default action on receiving this signal, which is to terminate. Thus, e.g. a C program
int main (int argc, char** argv) { int n = 5 / 0; }
...如果它甚至编译,将被默认 SIGFPE →
SIG_DFL
操作。相反,JVM的处理程序发出( catch
able) RuntimeException
,以便可以以本机看似的方式处理这些异常。
… if it even compiles, will be killed by the default
SIGFPE
→ SIG_DFL
action. The JVM's handler instead issues the (catch
able) RuntimeException
so that these exceptions can be handled in a native-seeming way.
正如其他几个人指出的那样,只是为了完整性,事实上从内核生成的
SIGFPE
通常从处理器本身的特殊中断映射;因此,管道类似于
As several others pointed out, and just for completeness, in point of fact the
SIGFPE
generated from the kernel is generally mapped from a special interrupt from the processor itself; thus, the "pipeline" is something like
- CPU错误陷阱中断→内核中断处理程序→
SIGFPE
SIG_DFL
→处理死亡
CPU error trap interrupt → kernel interrupt handler →
SIGFPE
SIG_DFL
→ process death
或
- CPU错误陷阱中断→内核中断处理程序→
JVM中的SIGFPE
处理程序→用户代码中的RuntimeException
ArithmeticException
CPU error trap interrupt → kernel interrupt handler →
SIGFPE
handler in JVM →RuntimeException
ArithmeticException
in user code
非-Unix平台的处理类似。
On non-Unix platforms the handling is analogous.
这篇关于Java如何处理除零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!