Implementation principle of Atitit exceptions and user business exceptions
Implementation principle of Atitit exceptions and user business exceptions
1.1. Implementation principle of exceptions 1
1.2. User Service exception 1
1.3. Exception translation and exception link 2
1.4. Avoid exceptions 2
1.5. Exception recovery 3
1.6. Capture exception handling process in the catch code block 3
1.1. Implementation principle of exceptions
Method invocation stack is used to track a series of method invocation processes in each thread. This stack stores the local information of each method called (such as the local variable of the method ). Each thread has an independent method call stack. For the main thread of a Java application, the bottom of the stack is the main () method of the program's entry (). When a new method is called, the Java Virtual Machine places the stack structure describing the method into the stack top. The method at the stack top is the method being executed.
When a method is successfully executed, the Java Virtual Machine pops up the stack structure of the method from the call stack, and then continues to process the previous method. If an exception is thrown during method execution, the Java Virtual Machine must find the catch code block that can capture the exception. It first checks whether the current method has such a catch code block. If so, it executes the catch code block. Otherwise, the Java Virtual Machine will pop up the stack structure of the method from the call stack, continue to the previous method to find the appropriate catch code block. During tracing, if the Java Virtual Machine finds a code block to handle this exception in a method, the stack structure of this method will become the top element of the stack, the program process is transferred to the exception handling code section of this method for further execution. When the Java Virtual Machine traces back to the method at the bottom of the Call Stack, if the code block still cannot be found to handle the exception, follow these steps.
(1) Call the printStackTrace () method of the exception object and print the exception information from the method call stack.
(2) If the thread is not the main thread, terminate the thread and other threads continue to run normally. If the thread is a main thread (that is, the main () method at the bottom of the method call stack), the entire application is terminated.
1.2.
User Service exception
User System
Pwd_or_uname_err
User_already_exist
Account System
Amount_not_ennagh
1.3.
Exception translation and exception chain
In a hierarchical software structure, there will be a top-down dependency, that is, the upper-layer subsystem will access the APIs of the lower-layer system. When subsystems on the upper layer do not need to relate to details of exceptions from the bottom layer, the common practice is to capture the original exception and convert it into a new exception of different types, A new exception is thrown, converted to a new exception of different types, and then thrown. This exception handling method is called exception translation.
From the object-oriented perspective, exception translation makes the exception type the same as the type of the object that throws the exception in the abstract layer. For example, when a car is running, a fault or exception occurs, while a staff member is late when driving to work. A car fault or exception is the cause of the employee's delay or exception, if an employee directly throws a car fault exception, it means that the car fault occurs to the employee. This is obviously unreasonable. The correct method is, translating a car exception in the employee category into a late arrival exception.
1.4.
Avoid exceptions
Avoid exceptions, especially running exceptions. There are two methods to avoid exceptions:
(1) Many runtime exceptions are caused by errors in the program code. You only need to modify the program code errors or improve the program implementation methods to avoid such errors.
(2) provide the status test method. Some exceptions are caused by improper operations when the object is in a certain state. For example, when the pressure of water vapor in the pressure cooker is very high, suddenly opening the lid will lead to an explosion. In order to avoid such accidents, the pressure cooker should provide the status test function so that users can determine whether the high-pressure steam in the pot is completely discharged before opening the lid. In a program, to call a method, you can use the state test function to test the function. Only when conditions are met can you call the method to avoid exceptions.
1.5.
Exception recovery
There are the following methods to maintain the atomicity of exceptions.
(1) The most common method is to check whether the parameters of the method are valid to ensure that the initial state of the object is not changed when an exception occurs (that is, before an exception occurs, check the conditions, make sure the exception does not occur before changing the object state ).
(2) Write a recovery code to explain the failures occurred during the operation and roll back the object state to the initial state. This method is not very common and is mainly used for permanentData StructureFor exampleDatabaseThis method is adopted in the transaction rollback mechanism.
(3) perform operations on the temporary copy of the object. After the operation is successful, copy the content in the temporary copy to the original object.
1.6.
Capture the exception handling process in the catch code block
As long as an exception occurs, a problem occurs in some places. Since the catch code block captures this exception, it should provide measures to handle the exception, such:
(1) Handling exceptions. Take some actions against the exception, such as making up for the loss caused by the exception or giving a warning.
(2) throw an exception again. After an exception is analyzed, the catch code block considers itself unable to handle it and throws an exception again.
(3) abnormal translation. Wrap the original exception as another exception suitable for the current abstraction layer, and then throw it.
(4) If you cannot take any action in the catch code block, do not catch the exception. Instead, use the throws clause to declare that the exception is thrown.
Java Exception Handling principles-Code-lover's Learning Notes-blog channel-CSDN.NET.html
Author: nickname: old wow's paw (full name: Attilax Akbar Al Rapanui Attila Akba Arla Panui)
Chinese name: atira (iron), EMAIL: 1466519819@qq.com
Reprinted please indicate Source: http://www.cnblogs.com/attilax/
Atiend