Does it simply check if divisor is different from zero every time there is division done (even in JIT-ed code)?
I mean how VM manages to throw an exception without being previously killed by the OS?
Does it simply check if divisor is different from zero every time there is division done (even in JIT-ed code)?
I mean how VM manages to throw an exception without being previously killed by the OS?
OS sends signal to the process. Default handler would stop the process, but you can define own handler for it. I bet java VM does.
The JVM catches the Division by Zero like this with C:
Compile and run it prints this:
The operating system synchronously raises a SIGFPE exception, the C program catches it, and then the java constructs and feeds you the ArithmeticException and cleans up after itself to stop the Java program.
See more about the signal returned here: http://publib.boulder.ibm.com/infocenter/javasdk/v6r0/index.jsp?topic=%2Fcom.ibm.java.doc.user.aix64.60%2Fuser%2Fsighand.html
In an Unix environment, in which division-by-zero is
signal
led viaSIGFPE
, the JVM will have installed a signal handler which traps theSIGFPE
and in turnthrow
s anArithmeticException
. If you're interested in the internals, see e.g.man signal
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… 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.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 likeSIGFPE
SIG_DFL
→ process deathor
SIGFPE
handler in JVM →RuntimeException
ArithmeticException
in user codeOn non-Unix platforms the handling is analogous.
Java handles the situation like any other language. A divide by zero error generates a processor exception which triggers an interrupt. The interrupt is "read" by the operating system and forwarded to the program if a handler is registered. Since Java registers a handler, it receives the error and then translates it into an
ArithmeticException
that travels up the stack.