Java exception/error mechanism
Javar
There are two main types of exceptions: checked exceptions and non-checked exceptions.
How to use exceptions? For what purpose?
Check exceptions are generally used at the API layer. The client must consider exceptions when calling statements, if an exception occurs, the client program must handle the exception based on its own scenario (record, throw the exception to the upper layer, jump to another processing policy ).
What are the more detailed considerations? Policy for throwing an exception to the upper layer? What are the policies for redirecting to other branches?
Abnormal record policy?
- The purpose of recording exceptions is to quickly locate the problem.
- Duplicate records are not required.
- Records where exceptions are initially generated.
- A unified style is required, and a log processing system is considered.
Throw an exception policy?
An exception has a hierarchical structure. Simply throwing an exception does not make any sense. The key is
- What is the overall processing process like?
- What is the overall call process?
What kind of exceptions will occur during a specific process can be pre-agreed.
For example,
- Exceptions at the UI rendering layer are closely related to users, and the exception information is usually not stack output.
- Exceptions at the business logic layer are closely related to the business and developers. In addition, this layer indirectly affects the exception handling at the UI Layer, usually, this layer of exception information contains the business-level and technical-level mixed exception information. This layer of exception information contains the business Tag Information and part of the stack information.
- The underlying technical support layer is closely related to the provided API callers. The exception information of this layer is mainly stack information.
This can be agreed!
We define a set of software projects with rules to constrain such a layered structure, the UI Layer, logic layer, and technical support layer define the exception handling classes for each layer (the class granularity can be freely controlled but must have rules ), when it comes to the filtering and transmission of abnormal data between layers, we can use inheritance and Polymorphism to smoothly complete class conversion, what we need is the exchange of exception information, so we don't need to care about which class it is. The role of a class is limited to the constraints at the code structure level.