当前位置:网站首页>Talking about the return format in the log, encapsulation format handling, exception handling
Talking about the return format in the log, encapsulation format handling, exception handling
2022-07-07 10:23:00 【thoughtCodes】
Don't talk about model selection , The main consideration is info Level of data :
General information :
As a general rule , We all encapsulate a return exception class on the project , Used to pack information .
Generally speaking, it is return code and return information , This is the most common .
In order to better check the problem , We designed it as follows :
000000- Business success
000001- Business failure, etc
about Msg Information? :
We can define the information format : Or information tips .
In this way, the corresponding packaging information can be output in different process information .
The information should have rules , Such as XX, reason , Easy to go online grep Search for
Generally, this is ok .
exception handling :
We know the level of exceptions :
We can define our own exception class , It's not commonly used , But you can do this .
It's more about exceptions provided by the framework :
Spring MVC There are the following 3 A way to handle exceptions :
Use Spring MVC The simple exception handler provided SimpleMappingExceptionResolver.
Realization Spring Exception handling interface HandlerExceptionResolver, Customize your own exception handler .
Use @ExceptionHandler Annotation implements exception handling
Inherit exceptions directly from the framework and handle . that will do .
Processing mode :
Direct selling , Leave it to the system .
Catch exceptions by yourself and handle .
Exception handling Basics
1.1 System.out.println It's expensive . call System.out.println Will reduce system throughput .1.2 Don't use abnormal printStackTrace() Method .printStackTrace By default, the call stack will be printed on the console , Accessing the console in a production environment is unrealistic .
Basic principles of exception handling
2.1 If you can't handle exceptions , Do not catch this exception .2.2 If you want to capture , It should be caught near the source of the divorce .
2.3 Don't swallow the anomalies you catch .
*( Is the exception caught , But do nothing )2.4 Unless you want to throw an exception again , Otherwise, take it. log get up .
2.5 When an exception is repackaged , And then throw it again , Do not print statck trace.
2.6 Use custom exception classes , Don't throw an exception every time you need to throw it java.lang.Exception. Method callers can use throws Know what exceptions need to be handled – So it is self describing .
2.7 If you write business logic , For errors that end users cannot fix , The system should throw non checked exceptions (unchecked exception); If you write a third-party package for other developers , For irreparable errors, use exceptions that need to be checked (checked exception).
2.8 Never write because throws Sentences will make you uncomfortable to use , Without declaring the exceptions that need to be checked .
2.9 Application level errors or irreparable system exceptions use non checked exceptions (unchecked exception) Throw out .
*( Attention is wrong , Means irreparable , For example, the configuration file is wrong )2.10 Organize your methods according to the granularity of exceptions
In short :log Printing exception ,, Custom exception , Pay attention to the detected abnormality .
Generally, you can print , The system has defaulted to exception .
Recommended reading :
https://www.cnblogs.com/langtianya/p/6931190.html
边栏推荐
- Fiddler simulates the interface test
- Why is the reflection efficiency low?
- Some test points about coupon test
- Study summary of postgraduate entrance examination in November
- Vs code specifies the extension installation location
- [higherhrnet] higherhrnet detailed heat map regression code of higherhrnet
- Wallys/IPQ6010 (IPQ6018 FAMILY) EMBEDDED BOARD WITH ON-BOARD WIFI DUAL BAND DUAL CONCURRENT
- Prototype object in ES6
- Guid primary key
- [sword finger offer] 42 Stack push in and pop-up sequence
猜你喜欢

The story of Plato and his three disciples: how to find happiness? How to find the ideal partner?

ORM -- database addition, deletion, modification and query operation logic

ArcGIS operation: converting DWG data to SHP data

Postman interface test V

JMeter about setting thread group and time
![[learning notes - Li Hongyi] Gan (generation of confrontation network) full series (I)](/img/94/b4df1ce2861a851fcd8de3e08540b0.png)
[learning notes - Li Hongyi] Gan (generation of confrontation network) full series (I)

Some properties of leetcode139 Yang Hui triangle

【学习笔记-李宏毅】GAN(生成对抗网络)全系列(一)

虚数j的物理意义

SolidWorks工程图中添加中心线和中心符号线的办法
随机推荐
Inno setup packaging and signing Guide
JMeter about setting thread group and time
【剑指Offer】42. 栈的压入、弹出序列
Download Text, pictures and ab packages used by unitywebrequest Foundation
Smart city construction based on GIS 3D visualization technology
【STM32】STM32烧录程序后SWD无法识别器件的问题解决方法
【acwing】789. Range of numbers (binary basis)
【HigherHRNet】 HigherHRNet 详解之 HigherHRNet的热图回归代码
ES类和对象、原型
HAL库配置通用定时器TIM触发ADC采样,然后DMA搬运到内存空间。
Parameter sniffing (2/2)
Some properties of leetcode139 Yang Hui triangle
[second on] [jeecgboot] modify paging parameters
Wallys/IPQ6010 (IPQ6018 FAMILY) EMBEDDED BOARD WITH ON-BOARD WIFI DUAL BAND DUAL CONCURRENT
Fiddler break point
Mongodb creates an implicit database as an exercise
Study summary of postgraduate entrance examination in September
求方程ax^2+bx+c=0的根(C语言)
Pdf document signature Guide
Google colab loads Google drive (Google drive is used in Google colab)