Atitit. Design principle of anomaly mechanism

Source: Internet
Author: User
Tags terminates

Atitit. design principle of anomaly mechanism

Defects

The key is to know that there is an exception table,TryThe range is the start and end of the record in the exception table row. JVMin theTryIf an exception is thrown in the live code interval, the entry instruction number of the exception record of the matching type is found in the exception table of the current stack, and then it is executed at that instruction. After execution of the exception instruction block, return to the following code. JVMretrieved in the order in which each entry appears in the table, if no matching items are found,JVMpops the current stack frame out of the stack and throws the same exception again. WhenJVMwhen the current stack frame pops up,JVMterminates the execution of the current method immediately, and returns to the method that called the method, but does not continue to execute the method normally, but throws the same exception in the method, which makesJVMThe same operation to search for the exception table is performed again in this method.
author:: Nickname:Old Wow's claws( Full Name::AttilaxAkbar Al Rapanui Attilaksachanui) Kanji Name:Ayron, email:[email protected]

Reprint please indicate source: http://www.cnblogs.com/attilax/


The above-mentioned inner layer method can not handle the exception of the layer to throw out, layer laminated stack, so as to form an exception stack. The exception stack is very useful for our dialysis problems, such ase.printstacktrace ();or with parameters.e.printstacktrace ();method to direct output of exception stack information to otherlog4jof thelog.error (Throwable)also has this effect. If at which level of the act the ability to deal with the anomaly has been, or untilJVM, directly causedJVMcrash out. such as whenMain ()The method also throws out the exception,JVMThe moment is at the end of life.

explain the operation of this byte code, if you usually look at the number of bytecode more students can skip this paragraph directly. The new directive with offset address 0 first finds the 58th constant in the constant pool, which is now a symbol reference of type Constant_class_info, The class parsing phase is translated into a direct reference to the Java.lang.Exception class, and then the virtual opportunity opens up a corresponding size instance space in the Java heap and pushes the reference of this space into the stack top of the stack. The DUP instruction with the offset of 3 simply copies the value of the top of the stack and pushes it back into the top of the stack, when there are 2 references to the new exception object in the Operation Stack. The invokespecial instruction with the offset of 4 references the first exception object to the stack, which invokes the instance constructor of the Excepiton class for the receiver, and a reference to the exception object is left on top of the stack. Write so much, plainly, that these 3 bytecode is dry "new Exception ()" This is what the Java code should do, and create any Java object without any difference. This portion of the time spent is analyzed in the previous section, where creating an exception object is only 20% of the time that the exception was created, thrown, and caught.  
followed by a 80%-time climax, offset to 7 of the athrow instruction, the operation of this instruction is basically to check the top of the Operation Stack, then the top of the stack must have a reference type of value, And is a subclass of Java.lang.Throwable (the virtual machine specification requires that if NULL is encountered as a NPE exception), and then temporarily put this reference out of the stack, and then search the exception table of this method (the exception table is what to write this paragraph), find out if this method can handle the exception of the handler, such as If a suitable handler is found, the PC register pointer will be reinitialized to the offset address of the first instruction of this exception handler. Then the current stack frame operation Stack is emptied, and then the reference just out of the stack into the stack. If in the current method is very tragic find handler, that had to put the current method stack frame out of the stack (the stack is the VM stack, do not mix with the previous Operation Stack, stack frame out of the stack means the current method exits), the method of the caller's stack frame is naturally on this thread VM stack top, And then to this new current method to do again just do the exception handler search, if still can't find, continue to kick the stack frame, so that until the search, or find a can use the handler, go to this handler the first instruction began to continue to execute, Either the stack frame of the VM stack is polished and the desired handler is not found, so the thread is forced to terminate and exit.

Java code

1. Exception table:

2. from to target type

3 .             8        8     class java/lang/exception  

The exception table above has only one handler record, which indicates the beginning of the offset address 0 (including 0), to the end of the offset address 8 (does not include 8), if there is a java.lang.Exception type of exception, then the PC register pointer to 8 start to continue execution. By the way, for the keyword catch and finally in the Java language, there is no special bytecode instruction in the virtual machine to support them, it is through the compiler to generate byte code fragments and different exception handlers to implement

    bytecode instructions There are 2 sentences left, and they're finished. The astore_1 instruction with the offset address of 8, the function is to put the value of the top of the stack in the first slot of the local variable table, just said that if an exception occurs, the virtual machine found the handler, will be the exception of the stack of the reference to the stack. So the purpose of this astore_1 implementation is to allow the code in the catch block to access the "E" defined by "catch  (Exception e)", and incidentally, the local variable table starts at 0, and the No. 0 slot is a reference to the method receiver. , which is the object that can be accessed using this key. The last return instruction does not have to be more, it is the return instruction of the void method, because there is no content in our catch block, so we return immediately.  
     so far, these bits of bytecode are done, and we'll summarize what the virtual machine might do in the Athrow directive (just one part of it):  

·  Check the top of the stack exception object type (check is not NULL, whether the referance type, whether the subclass of Throwable is generally done in the data flow analysis of the class validation phase, or simply do not rely on the compiler to ensure that, compile-time writes to the Code property's stackmaptable, only type validation at load time )

·  to stack a reference to an exception object

·  Search the exception table to find a matching exception handler

·  Resetting the PC register status

·  cleanup Operation Stack

·  to put a reference to the exception object into the stack

·  The stack frame of the exception method is stacked on a per-stack basis (the stack here is the VM stack)

·  brutally terminates the current thread.

·  ...

The first sentence extracts the exception object referenced in the Operation Stack, the second sentence checks if the exception is empty, and the virtual machine specification requires NULL for the NPE exception, which is the implementation of this sentence:  

Note that you can use "common code" refers to the code in Handle_return, each opcode processing will go to this code. Because the exception does not necessarily come from the Athrow directive, which is not necessarily from the user program directly thrown, the virtual machine operation will also produce an exception, such as by 0, null pointer, a serious point of the oom god Horse. So after the exception of the method exit action in the general Handle_return inside according to pending_exception processing, code too much will not post. The first few words do not have too special action, it seems that the key implementation of the Athrow command is still in the Handle_exception section, see its code (for the sake of clarity, I deleted unnecessary code, such as support for tracking debugging statements)

Look at the key parts of this code, CALL_VM that is to find the exception table, the execution of the Interpreterruntime::exception_handler_for_ Exception in the same directory of InterpreterRuntime.cpp, find the specific process is a little complicated, just look at the main thread of the program, the code here is no longer involved in. If found, that is if  (CONTINUATION_BCI >= 0) is established, (BCI means bytecode index, byte code index

See exception nature through the JVM-Java synthesis-java-iteye forum. htm

How the JVM handles Java exceptions - Asahi's Column - Blog channel -CSDN.NET.htm

Atitit. Design principle of anomaly mechanism

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.