Deeply understand the exception handling mechanism of Java

Exceptions refer to unexpected conditions, such as file missing, network connection failure, illegal parameters, etc. An exception is an event that occurs during program operation and interferes with the normal instruction flow. Java describes various exceptions through many subclasses of the throwable class in the API. Therefore, Java exceptions are objects, instances of throwable subclasses, and describe the error conditions that appear in a piece of code. When the condition is generated, the error throws an exception.

Java exception class hierarchy diagram:

Figure 1 Java exception class hierarchy

In Java, All exceptions have a common ancestor throwable. Throwable specifies the commonality of any problem transmitted through Java applications through the exception propagation mechanism available in the code. Throwable: has two important subclasses: exception and error. Both are important subclasses of Java exception handling, and each contains a large number of subclasses.

Error: it is an error that cannot be handled by the program, indicating a more serious problem in running the application. Most errors have nothing to do with the operation performed by the coder, but indicate a problem with the JVM (Java virtual machine) when the code is running. For example, Java virtual machine running error (virtual machineerror). Outofmemoryerror will appear when the JVM no longer has the memory resources required to continue the operation. When these exceptions occur, the Java virtual machine (JVM) will generally choose thread termination.

。 These errors indicate that the failure occurred in the virtual machine itself or when the virtual machine tried to execute the application, Such as Java virtual machine running error and class definition error (NoClassDefFoundError), etc. these errors are not traceable because they are outside the control and processing capacity of the application, and most of them are conditions that are not allowed to occur when the program is running. For a reasonably designed application, even if an error does occur, it should not try to deal with the abnormal conditions caused by it. In Java, errors are common Subclass description of error.

Exception: an exception that can be handled by the program itself.

The exception class has an important subclass runtimeException. The runtimeException class and its subclasses represent errors caused by "JVM common operations". For example, if you try to use a null object reference, divide by zero, or array out of bounds, run-time exceptions (NullPointerException, arithmeticexception) and arrayindexoutofboundexception are thrown, respectively.

Note: the difference between exceptions and errors: exceptions can be handled by the program itself, while errors cannot be handled.

Usually, Java exceptions (including exceptions and errors) are divided into checked exceptions and unchecked exceptions (exceptions that must be handled by the compiler): reasonable exceptions that are easy to occur when the correct program is running. Although the verifiable exception is an exception, its occurrence can be predicted to a certain extent, and once this exception occurs, it must be handled in some way.

Except runtimeException and its subclasses, other exception classes and their subclasses belong to searchable exceptions. The characteristic of this exception is that the java compiler will check it, that is, when this kind of exception may occur in the program, either catch it with a try catch statement or throw it with a throw clause declaration, otherwise the compilation will not pass.

Non observable exceptions (exceptions that the compiler does not require forced handling): including runtime exceptions (runtimeException and its subclasses) and errors (errors).

Exceptions are divided into two categories: runtime exceptions and non runtime exceptions (compilation exceptions). The program should handle these exceptions as much as possible.

Runtime exceptions: they are all exceptions of runtimeException class and its subclasses, such as NullPointerException (null pointer exception), indexoutofboundsexception (subscript out of bounds exception), etc. these exceptions are not checked. The program can choose to capture and handle them or not. These exceptions are generally caused by program logic errors. The program should avoid such exceptions as much as possible from a logical point of view.

The characteristic of runtime exception is that the java compiler will not check it, that is, when such an exception may occur in the program, it will be compiled even if it is not caught with the try catch statement or thrown with the throw clause declaration. Non runtime exception (compilation exception): it is an exception other than runtimeException, which belongs to exception class and its subclasses. From the perspective of program syntax, it is an exception that must be handled. If it is not handled, the program cannot be compiled. For example, IOException, sqlexception and user-defined exception exceptions, exception checking is not customized in general.

In Java applications, the exception handling mechanism is to throw exceptions and catch exceptions.

Throw exception: when a method throws an exception due to an error, the method creates an exception object and delivers it to the runtime system. The exception object contains exception information such as exception type and program state when the exception occurs. The runtime system is responsible for finding and executing the code to handle exceptions.

Catch exception: after the method throws an exception, The runtime system will turn to finding the appropriate exception handler (exception handler). A potential exception handler is a collection of methods stored in the call stack in turn when an exception occurs. When the exception type that the exception handler can handle is consistent with the exception type thrown by the method, it is the appropriate exception handler. The runtime system starts with the method with the exception, and checks the methods in the call stack in turn until it finds the method with the appropriate exception The method of the constant processor is and executed. When the runtime system traverses the call stack without finding a suitable exception handler, the runtime system terminates. At the same time, it means the termination of Java programs.

For runtime exceptions, errors or verifiable exceptions, Java technology requires different exception handling methods.

Due to the non availability of runtime exceptions, in order to more reasonably and easily implement the application, Java stipulates that runtime exceptions will be automatically thrown by the Java runtime system, allowing the application to ignore runtime exceptions.

For errors that may occur during method operation, Java allows the method not to make any throw declaration when the running method does not want to catch. Because most error exceptions belong to situations that can never be allowed to occur, and they are exceptions that reasonable applications should not catch.

For all verifiable exceptions, Java stipulates that a method must catch or declare that it is outside the thrown method. That is, when a method chooses not to catch a traceable exception, it must declare that it will throw an exception.

The method that can catch exceptions needs to provide an exception handler of the corresponding type. The caught exception may be an exception thrown by its own statement, or an exception thrown by a called method or Java runtime system. In other words, the exception that a method can catch must be the exception thrown by java code somewhere. In short, exceptions are always thrown first and then caught.

Any Java code can throw exceptions, such as code written by itself, code from Java development environment package, or Java runtime system. Anyone can throw an exception through Java's throw statement.

Any exception thrown from a method must use the throws clause.

Catch exceptions through try catch statements or try catch finally statements.

Generally speaking, Java stipulates that for verifiable exceptions, they must be caught, declared or thrown. It is allowed to ignore non traceable runtimeException and error.

1. Catch exceptions: try, catch and finally

1. Try catch statement

[java] copy

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>