Java Virtual Machine knowledge [abnormal]

abnormal

  Example Java Virtual Machine exception Throwable or subclass to said program is an instant control of the Throws essentially nonlocal conversion, i.e., conversion from place to place thrown exception handling.

  Cause of an exception

  • Athrow bytecode instruction is executed.
  • Thrown after the virtual machine program detects the occurrence of the sync abnormal implementation, when the abnormal bytecode instruction will immediately abnormal in the event of implementation. E.g:

  Each method executed by the Java virtual machine will be equipped with zero to more exception handler . The exception handler is described in which the method code valid scope , can handle the type of exception and exception handling code location .

  If the current exception handler does not find any method, and the current does occur during the method call exception, that the current method of operand stack and local variable table are discarded, then its corresponding stack frame the stack, and returned to the method stack frame of the caller. Unprocessed exception will be thrown back in the caller's stack frame method, and continue to repeat the process described above in method calls throughout the chain. If you have reached the top of the method call chain, still not found a suitable exception handler to handle the exception, the whole thread of execution will be terminated.

  The search order is critical when searching exception handler in the class file, the exception handler for each method are stored in a table. Runtime, when an exception is thrown, the order exception handler Java virtual machine in the class file exception handler table described, from front to search.

  Java virtual machine itself will not process exception handler table is sorted or otherwise forced to deal with, so the Java language exception handling semantics, in fact, by the compiler appropriate exception handler in order to assist in arranging the table Completed.

Examples of the try-catch-finally

public class Test {
  void testException(int i) {
    try {
      System.out.println(i);
    } catch (NullPointerException e) {
      throw new NullPointerException();
    } catch (RuntimeException e) {
      throw new RuntimeException();
    } finally {
      System.err.print(i);
    }
  }
}

  The above example of compiling javac Test.javaand decompiling javap -c Test.classgenerate byte code command.

0: getstatic     #2 // 获取第一个静态方法 println:Field java/lang/System.out:Ljava/io/PrintStream;
3: iload_1
4: invokevirtual #3 // 执行第一个静态方法 println:Method java/io/PrintStream.println:(I)V
7: getstatic     #4 // 获取finally的静态方法 print:Field java/lang/System.err:Ljava/io/PrintStream;
10: iload_1
11: invokevirtual #5 // 执行finally的静态方法 print:Method java/io/PrintStream.print:(I)V
14: goto          45 // 跳到return指令结束程序
==========================
17: astore_2
18: new           #6 // class java/lang/NullPointerException
21: dup
22: invokespecial #7 // Method java/lang/NullPointerException."<init>":()V
25: athrow
26: astore_2
27: new           #8 // class java/lang/RuntimeException
30: dup
31: invokespecial #9 // Method java/lang/RuntimeException."<init>":()V
34: athrow
35: astore_3
36: getstatic     #4 // Field java/lang/System.err:Ljava/io/PrintStream;
39: iload_1
40: invokevirtual #5 // Method java/io/PrintStream.print:(I)V
43: aload_3
44: athrow
==========================
45: return
Exception table:        // 异常表,顺序和源码一致
from    to    target    type
   0     7    17   Class java/lang/NullPointerException //如果0-7之间有NullPointerException异常,跳到17
   0     7    26   Class java/lang/RuntimeException  //如果0-7之间有RuntimeException异常,跳到26
   0     7    35   any      //0-7之间任意情况下都会到35行,即finally
  17    36    35   any      //17-36之间任意情况下都会到35行,即finally

Reference: "in-depth understanding of the Java Virtual Machine (Second Edition)," "Java Virtual Machine Specification (Java SE version 8)."

Guess you like

Origin www.cnblogs.com/bigshark/p/11223550.html