java - 请问业务层方法是抛出一个异常好还是返回一个结果更好
阿神
阿神 2017-04-17 16:42:05
0
9
854
阿神
阿神

闭关修行中......

reply all(9)
大家讲道理

My design is generally that the DAO layer does not handle any exceptions. The Service layer calls the DAO layer and catches the exception, and then returns the information to the Action layer through a status code.
Personally, I feel that the logic of the Action layer should be as simple as possible, because Action is the interface layer, and it is too complicated to see through the functions provided by the interface at a glance.

伊谢尔伦

Error codes are very common in procedural languages, but in object-oriented procedures, exceptions are used to handle them a little more.
The disadvantages of using error codes are:
1. Error detection is not mandatory
2. The code is full of various if else error code judgments
The advantages of using exceptions are:
1. Error detection is mandatory, you Exceptions must be handled or thrown up
2. The code does not need to judge various status codes. If there is an exception, it will be thrown directly and terminate the operation. 3. There is a stack for tracking errors.

巴扎黑

The library throws an exception and the business returns an error code.

黄舟

I think this mainly depends on two points: 1. The amount of logic in each layer needs to be chosen by yourself; 2. Personal style, I prefer to handle it as much as possible in the business layer, but the dao layer only throws exceptions. In fact, it can be processed anywhere, as long as it is not returned directly to the client.

阿神

The business layer is all if else. Basically, the default value should be set before returning the data, such as null value. Only the dao class may interact with systems other than the business layer. The probability of abnormality in the database and network is greater, so it should be in the dao class. Properly handle exceptions in the business layer/action and return null/default values, which is more user-friendly

刘奇

Persistence is generally not logically processed. It is best to throw it to the upper level, otherwise you will have to use some persistence framework in the future. It will be too troublesome to transplant.
Although there are many situations that cause exceptions, the results for users are It was not successful. As for why it was not successful, it is left to the developers to see, so I think there is no need to return so many results and directly return the exception. The developer can also judge the cause by looking at which exception
I think you should record this for Determine what went wrong if something went wrong, right? Printing logs should be a common practice, and add exceptions and the like to the logs

迷茫

How many levels are nested?
Learn to reduce nesting first

In addition, the service layer returns a certain result. It handles exceptions and business logic.
The action layer plays the role of routing. Just call the service and return it directly.

左手右手慢动作

I asked this question on Zhihu: https://www.zhihu.com/question/36278363
Please refer to it.

洪涛

It doesn’t matter in your example,
But when there are things, it does matter.
If you are using declarative things, then an error occurs in an intermediate link in the transaction, because the error is thrown directly, so you can start the transaction That layer is caught and rolls back all operations in the current transaction.
If you have caught an exception in dao, then it will be very troublesome to sense whether the business needs to be rolled back. You need to perform the results of each dao operation. judge.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template