Programming architecture (02): Java exception architecture

Source code of this article: GitHub · click here | gitee · click here

1、 Anomaly introduction

Excellent program code pursues efficiency, security and low error rate, but exceptions in the program are inevitable. Reducing the frequency of exceptions is the key. How to deal with exceptions is another important aspect. The exception framework in Java system is very important for system development.

Don't panic in the face of system exceptions. Although exceptions are errors, they are also messages sent by the system to identify system defects and areas that need to be improved.

2、 API system

Many exception classes have been defined in the Java API, which are divided into two categories: error and exception. Throwable is the superclass of all exceptions, as shown in the figure:

Error: it is generally an unrecoverable class at the bottom. Generally, such errors are serious, and the JVM will terminate its running thread;

Exception: an exception that the program itself can catch and preprocess, such as catching or throwing;

Several classic and common runtimeexceptions are as follows: null pointer NullPointerException; Array subscript out of bounds, ArrayIndexOutOfBoundsException, etc.

3、 Exception handling

Java exception handling Keywords: try, catch, finally, throw, throws.

Exceptions should be handled in appropriate places. The exception handling criteria are as follows: who knows who handles it, who is responsible for who handles it, and who causes who handles it.

1. Throw exception

That is, exceptions are not handled in the current process. One is passed directly to the caller through the method. The throws keyword is used to declare the type of exception thrown on the method declaration, and multiple types of exceptions can be declared and thrown at one time. Throw keyword is used to throw an exception object inside a method. It often throws a prompt during business verification.

It should be noted that in the spring framework, transaction triggering is mostly handled by whether to throw an exception. If the method is within the transaction control and the exception in the method is caught but not thrown in the end, the transaction is invalid.

2. Catch exception

The try catch finally keyword is usually used to catch exceptions

Try trying to catch exception:

If the statements are executed in sequence, skip catch. If there is a finally code block, execute it. Otherwise, execute the subsequent process;

If an exception is caught, the type in the catch is matched. If there is no matching catch type, the exception is handed over to the JVM for processing. Finally, the code will be executed, and the code after the process will not be executed;

If an exception is caught and there is a matching catch type, skip to the catch code block for execution, and the finally code will be executed. After the finally code block is executed, continue to execute the subsequent code;

Catch matches the possible exception types and compensates them. For example, if an exception occurs, an exception status needs to be updated. If there is no catch block, it must be followed by the finally block to handle resource release;

Finally, whether or not an exception is caught, the finally code will be executed, which is also one of the common exception problems in the interview. For example, in the finally code block return, or modifying the return value, it mainly involves value passing and reference passing.

3. Exception log

The essential function of complex business system, exception log system, is used to analyze operation problems. As the core basis for continuous system optimization, it is usually recorded as follows:

After the exception log is recorded, the task analysis will be carried out regularly to constantly find the places where the system is prone to problems, and then continue to improve and optimize.

4. Fuse degradation

In the microservice architecture system, a service failure or abnormality triggers the fusing of the service to avoid causing the abnormality of the whole microservice link and preventing the avalanche of the services of the whole system. In order to alleviate the pressure of server resources and ensure the normal operation of core business.

4、 Source code address

GitHub·地址
https://github.com/cicadasmile
GitEE·地址
https://gitee.com/cicadasmile
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
分享
二维码
< <上一篇
下一篇>>