Java Virtual Machine Specification read (c) exceptions

Source: Internet
Author: User


exceptions to the Java Virtual machine are represented by an instance of Throwable or its subclasses, which is actually a real-time, non-local (nonlocal) conversion of the control of the program-from where the exception is thrown to where it handles the exception.

The vast majority of exceptions are caused by an operation that is performed by the current thread, which can be referred to as a synchronous exception. In contrast, an asynchronous exception is an exception that is caused by an action that is performed anywhere else in the program. The presence of exceptions in a Java Virtual machine is always caused by one of the following three reasons:

1. Virtual machine synchronization detects an abnormal execution of the program, and the exception will be thrown immediately after the byte-code instruction for the non-normal execution occurred. For example:

    • The operations contained in bytecode directives violate the semantics of the Java language, such as accessing an element that extends beyond the bounds of an array.

    • The class encountered an error loading or linking.

    • Resource constraints, such as too much memory, are used when using certain resources.

The 2.athrow bytecode instruction is executed.

3. The occurrence of an asynchronous exception is caused by the following reasons:

    • The Stop method for thread or threadgroup is called.

    • An internal program error implemented by the Java virtual machine.

When a thread calls the Stop method, it affects other threads, or all threads in the thread group. Exceptions that occur in other threads at this time are asynchronous exceptions, because these exceptions can occur anywhere in the program's execution. The internal exception of a virtual machine is also considered an asynchronous exception

The Java Virtual Machine specification allows additional small pieces of code to be executed when an asynchronous exception is thrown, allowing the code optimizer to detect and throw ① where it can be handled without violating the semantics of the Java language.

An exception is thrown in the Java virtual machine is a precisely defined program control transfer process, when the exception is thrown, the control of the program to transfer the moment, all in the location of the exception thrown before the byte code instructions produced by the ② should be observable, and in the location of the exception thrown by the bytecode instruction , it should have not been executed. If the code executed by the virtual machine is ③ by the optimized code, some code after the occurrence of the exception may have been executed, and the optimized code must ensure that the impact of their advance execution is not visible to the user program.

each method executed by a Java Virtual machine is equipped with 0 to many exception handlers (Exception handlers), which describes its effective scope in the method code (described by the byte-code offset range), The type of exception that can be handled and where the code that handles the exception is located. To determine whether an exception handler can handle a specific exception, you need to check that the location of the exception is within the effective scope of the exception handling and that the exception that occurs is that the handler is the one that can handle the exception type or its subtype two conditions. When an exception is thrown, the Java Virtual machine searches for the respective exception handlers for the current method, and if it can find an exception handler that can handle the exception, it shifts the control of the code to the branch of the exception handler described in handling the exception.

The search order when searching for an exception handler is critical, and in the class file, the exception handler for each method is stored in a table. At run time, when an exception occurs, the Java virtual machine follows the Exception processor table in the class file to describe the order of the exception handlers, searching from front to back.

It is important to note that the Java virtual machine itself does not sort or otherwise enforce the exception handler table for the method, so the semantics of exception handling in the Java language are actually facilitated by the compiler arranging the order of the exception handlers in the table appropriately. An explicit exception handler lookup sequence is defined in the class file to ensure that the Java virtual machine performs consistent behavior regardless of the path that the class file is generated from.

This article is from the "attack on the Program Ape" blog, please be sure to keep this source http://zangyanan.blog.51cto.com/11610700/1855720

Java Virtual Machine Specification read (c) exceptions

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.