简体   繁体   中英

How does JVM “throw” an exception

I know that JVM has an exception table that maps possible exceptions that can be thrown in given bytecode indexes. I also read that athrow bytecode throws excetion of reference type that is present on the top of the stack. My question relates more to how instructions like irem "throw" exceptions.

Does the JVM check the top of stack after every instruction execution to check if there is an exception? Would appreciate any insight on that is happening under the hood.

irem is the "logical int remainder" operator. The Java Virtual Machine specification writes :

Run-time Exception

If the value of the divisor for an int remainder operator is 0, irem throws an ArithmeticException .

How the JVM implementation accomplishes that is not specified. It can instruct the CPU to compare the divisor with zero before performing the division, or perfom the division, and react to however the CPU in question signals that a division by 0 occured. As divisions by zero are likely rare, the latter strategy is probably more efficient.

For instance, the Intel 64 and IA-32 architectures software developer's manual combined volumes 3A, 3B, and 3C: System programming guide writes:

6.1 INTERRUPT AND EXCEPTION OVERVIEW

Exceptions occur when the processor detects an error condition while executing an instruction, such as division by zero. The processor detects a variety of error conditions including protection violations, page faults, and internal machine faults. The machine-check architecture of the Pentium 4, Intel Xeon, P6 family, and Pentium processors also permits a machine-check exception to be generated when internal hardware errors and bus errors are detected.

When an interrupt is received or an exception is detected, the currently running procedure or task is suspended while the processor executes an interrupt or exception ha ndler. When execution of the handler is complete, the processor resumes execution of the interrupted procedure or task. The resumption of the interrupted procedure or task happens without loss of program continuity, unless recovery from an exception was not possible or an interrupt caused the currently running program to be terminated.

The JVM would therefore define that exception handler to create an exception object, leave a reference to it in a well known register, and then proceed as for an athrow bytecode instruction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM