The content of this article is about how to use JVM to catch Java exceptions? (With examples), it has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
1. Two key factors of exceptions
(1) Throwing exceptions
1. Explicit: The application manually throws exceptions. Specifically, use throw to throw an exception
2. Implicit: The Java virtual machine automatically throws an exception for code that cannot be executed
(Recommended:Java Tutorial)
(2) Catching exceptions
1.try code block: used to mark code that requires exception monitoring.
2.catch code block: following the try code block, used to catch a specified type of exception triggered in the try code block. In addition to declaring the type of exception being caught, the catch block also defines an exception handler for that exception type. In Java, a try code block can be followed by multiple catch code blocks to catch different types of exceptions. The Java Virtual Machine matches exception handlers from top to bottom. Therefore, the exception type captured by the previous catch code block cannot cover the following one, otherwise the compiler will report an error.
3.fnally code block: Following the try code block and catch code block, it is used to declare a piece of code that must be run. It is designed to avoid skipping certain critical cleanup code, such as closing open system resources. Under normal program execution, this code will run after the try block. Otherwise, that is, when the try code block triggers an exception, if the exception is not caught, the fnally code block will run directly and rethrow the exception after running. If the exception is caught by a catch block, the finally block is run after the catch block. In some unfortunate circumstances, the catch code block also triggers an exception, then the fnally code block will also run and throw the exception triggered by the catch code block. In some extremely unfortunate situations, the fnally code block also triggers an exception, so you have to interrupt the execution of the current fnally code block and throw the exception.
2. Classification of exceptions
1.The parent class of all exceptions is Throwable
2.Error exception is The execution state of the program cannot be restored. It can only terminate the thread or even terminate the JVM.
3.Exception is a less serious exception than Error
4.Runtime Exception and Error are exceptions that do not need to be checked
5. Except for Runtime Exception and Error, all exceptions are Check Exception
6. Check Exception exceptions are exceptions that need to be explicitly caught
It is very expensive for the Java virtual machine to create exception instances. The virtual machine needs to generate a stack trace of the exception. This operation will access the Java stack frames of the current thread one by one and record various debugging information, including the name of the method pointed to by the stack frame, the class name and file name of the method, and the line in the code where the exception was triggered.
Since the construction of exception instances is very expensive, can we cache exception instances and throw them directly when needed? From a grammatical perspective, this is allowed. However, the stack trace corresponding to this exception is not the location of the throw statement, but the location of the new exception.
Therefore, this approach may mislead developers into targeting the wrong location. This is why in practice, we often choose to throw new exception instances.
Exception handler
1. Source: Each method will generate an exception table during compilation. Each entry in the exception table represents an exception handler.
2. Composition:
(1) from pointer, to pointer: represents the scope of catching exceptions, which is the scope of Try.
(2)target pointer: represents the starting position of the processor, which is the starting position of the catch.
(3)The exception type caught.
3. Catching exceptions
(1) When the program triggers an exception, the Java virtual machine traverses all entries in the exception table from top to bottom. When the index value of the bytecode that triggers the exception is within the monitoring range of an exception table entry, the Java virtual machine determines whether the exception thrown matches the exception that the entry wants to catch. If there is a match, the Java Virtual Machine transfers control flow to the bytecode pointed to by the entry's target pointer.
(2) If all exception table entries are traversed and the Java virtual machine still does not match the exception handler, it will pop up the Java stack frame corresponding to the current method and repeat the above operations in the caller. In the worst case, the Java virtual machine needs to traverse the exception table of all methods on the current thread's Java stack.
4. Compilation of finally code: The current version of the Java compiler copies the contents of the fnally code block and places them in the exits of all normal execution paths and exception execution paths of the try-catch code block.
代码1: Try{ Try block } catch { Catch block } finally { Finally block }
代码2: Try { Try block Finally block } catch { Catch block Finally block } finally{ Finally block }
Code 1 is our Java code, code 2 is the compiled Java code.Note: If the catch code block catches an exception and triggers another exception, which exception is caught and rethrown by fnally? The answer is the latter. In other words, the original exception will be ignored, which is very detrimental to code debugging.
As mentioned in the previous section, catch exceptions will be ignored. Java7 introduced the problem of Supressed exception handling. But it is still very troublesome to use (no experience,
The above is the detailed content of How to use JVM to catch Java exceptions? (with examples). For more information, please follow other related articles on the PHP Chinese website!