Java Virtual Machine JVM-Interview Questions

1. How does the Java virtual machine catch exceptions?

answer:

In the compiled bytecode, each method is accompanied by an exception table. Each entry in the exception table represents an exception handler and consists of a from pointer, a to pointer, a target pointer and the captured exception type. The value of these pointers is the bytecode index (bytecode index, bci), which is used to locate the bytecode.

Among them, the from pointer and to pointer mark the scope monitored by the exception handler, such as the scope covered by the try code block. The target pointer points to the starting location of the exception handler, such as the starting location of the catch code block. 

When an exception occurs, the JVM will traverse all entries in the exception table. If it is found that the location of the exception is within the from-to range of an entry, it will compare whether the thrown exception and the caught exception are consistent. If it is consistent, jump to the start position of the exception handler pointed to by the target pointer for execution. Once the exception table of the method is not found, the stack frame corresponding to the method will be popped up, and the same processing will be performed on the method that called the method.

The worst case scenario is that there is no matching exception even after traversing the entire thread stack.

2. Why is finally always executed?

(1) From the corresponding bytecode content of Code, it can be seen that the finally code block is copied under various possible branches (redundant design), and will always be executed without exception;

(2) It can be concluded from the exception table that when an exception occurs, it will first jump to the catch for execution. After the catch is executed, it will jump to finally for execution. So finally will always be executed.

Example:

First of all, it can be seen from the exception table that if an exception occurs in the range of 0 to 4 (excluding 4), it will immediately jump to position 7 to handle the exception; in addition, no matter an exception occurs in the range of 0 to 4 (try block), it must jump to position 15 for execution. The code in the finally block; in addition, no matter whether an exception occurs within the range of 7 ~ 12 (catch block), the finally block must be executed at position 15.

3. Please briefly describe the structure of the JVM runtime data area and the functions of each part?

4. Tell me about the function of the program counter ?
5. Java memory area? Where are the local variables?

Guess you like

Origin blog.csdn.net/puzi0315/article/details/129350544