"In-depth understanding of the Java Virtual Machine" - JVM how exception handling

A, Java abnormal

In a program, the programmer errors may occur in unexpected circumstances or environment outside the controllable range of the programmer, for example, bad data user, trying to open a file does not exist, and the like. In order to be able to handle run-time errors in a timely and effective program, Java introduces a special exception class.

Two, Java common exceptions Category

Third, why an exception

An exception is generated in Java, mainly in the following three reasons:

  1. Exceptions Exception, Java Virtual Machine Java generated an internal error.
  2. Abnormal program code written in an error generated, for example, a null pointer exception, abnormal array bounds. This anomaly is called abnormal unchecked, generally need to focus to handle them in some classes.
  3. Generated by a manual throw statement abnormality, such abnormality called abnormality check, typically used to inform the caller of the method of the necessary information.

Fourth, how do encounter abnormal?

We generate an exception object and submit it to the runtime system is called throw (throw) exception. The system looks at run-time call stack method, able to handle exceptions until you find the type of object, which is a process known as capture (catch) exceptions.

Java exception forces the user to consider the robustness and security of the program. Exception handling should not be used to control the normal flow of the program, its main role is to capture a program exception occurs during operation and corresponding processing. Write code to handle exceptions a method that may arise, we can follow the following three principles:

  1. Used in the current method declaration try catch statement to catch the exception.
  2. When a method is covered, covering it the same method must throw an exception or exception subclass.
  3. If the parent class throws more exceptions, the override method must throw a subset of those unusual, but can not throw a new exception.

(Reference: http: //c.biancheng.net/view/1038.html)

Fifth, the JVM perspective generating abnormal expression

 Look at sample code:

public class Foo {
  private int tryBlock;
  private int catchBlock;
  private int finallyBlock;
  private int methodExit;


  public void test() {
    try {
      tryBlock = 0;
    } catch (Exception e) {
      catchBlock = 1;
    } finally {
      finallyBlock = 2;
    }
    methodExit = 3;
  }
}

This code is a simple piece of exception handling code, we can see the expression in the form of class files by javap:

public void test();
    descriptor: ()V
    flags: ACC_PUBLIC
    Code:
      stack=2, locals=3, args_size=1
         0: aload_0
         1: iconst_0
         2: putfield      #2                  // Field tryBlock:I
         5: aload_0
         6: iconst_2
         7: putfield      #3                  // Field finallyBlock:I
        10: goto          35
        13: astore_1
        14: aload_0
        15: iconst_1
        16: putfield      #5                  // Field catchBlock:I
        19: aload_0
        20: iconst_2
        21: putfield      #3                  // Field finallyBlock:I
        24: goto          35
        27: astore_2
        28: aload_0
        29: iconst_2
        30: putfield      #3                  // Field finallyBlock:I
        33: aload_2
        34: athrow
        35: aload_0
        36: iconst_3
        37: putfield      #6                  // Field methodExit:I
        40: return
      Exception table:
         from    to  target type
             0     5    13   Class java/lang/Exception
             0     5    27   any
            13    19    27   any
      LineNumberTable:
        line 10: 0
        line 14: 5
        line 15: 10
        line 11: 13
        line 12: 14
        line 14: 19
        line 15: 24
        line 14: 27
        line 16: 35
        line 17: 40
      StackMapTable: number_of_entries = 3
        frame_type = 77 /* same_locals_1_stack_item */
          stack = [ class java/lang/Exception ]
        frame_type = 77 /* same_locals_1_stack_item */
          stack = [ class java/lang/Throwable ]
        frame_type = 7 /* same */

Bytecode can be seen from comments, finally block is added to the three places. That is, when translating from the java code into byte code file, generated JVM finally block in the logical block and the catch block to try. But think about it, why three "finally" it? The last exception is finally prepared occurred while executing the code in the catch block. So, one may ask, if there is code in the finally block error how to do it? Here, the introduction of no facts to be proven himself: to be thrown out, a layer of code to deal with.

Here explain the yellow portion of the byte code:

exception table table indicating an abnormality, exception table for storing all the code related to the abnormality, after compiling each class, will follow an exception table, if an exception occurs, the corresponding row looks first in the exception table (i.e., a respective tag the  try{}catch(){}code blocks), if found, execution jumps to the exception handler, if not found, then (after the finally return), and applied to the parent abnormal copy the caller, then the call to query the parent table, and so on .

from ... to: monitor exception handler represents the range (for example try block comprising code)

target: the exception handler represents a starting position (such as the catch block comprising code)

type: it is Exception Handling

Then, after an exception occurs, how to control exception table?

When the program trigger an exception, Java virtual machine from top to bottom traversal abnormal entries in the table. Abnormal match when the index value bytecode trigger an exception within the scope of monitoring an exception table entries, Java virtual opportunity to judge the exceptions thrown and the entry you want to capture. If the match, Java virtual machine will control the flow to a target on the code entry pointer, the program continues to run.

Mentioned below, the analytical about bytecode exception table:

The program starts, runs to 1: iconst_0, the Exception exception occurs, then the program will go to a convenient method table, from the first line, the detected 0 <a <5, the detection range corresponds to the first entry, and then check again throws the abnormal exception, entry matches the captured exception process, after the jump to the bytecode number 13 continues to run. If more in 14: When an exception occurs aload_0, and the program jumps to the exception table, look for unusual entries match, the final target is to find the serial number for the byte code 27, then it has completed all down to bytecode.

Examples of the belonging abnormality occurs in the catch block, it will be seen there is a step after athrow bytecode, which is thrown out of it.

 

Well, Jvm see an exception to this.

(Cited: geeks time)

Guess you like

Origin www.cnblogs.com/chenscript/p/11391690.html