java - 项目中自定义异常用一个还是多个?
高洛峰
高洛峰 2017-04-17 13:07:48
0
4
924
高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(4)
Ty80

Of course, different exceptions are defined according to the specific situation. Define only one exception? Never heard of it.

左手右手慢动作

You can control the granularity yourself. It’s also very tiring to divide it too finely. For example, you can use an exception class for input exceptions, and then use different error codes/messages to represent different situations (the input number is too large, too small, cannot have decimals, cannot be odd, etc...)

Can you imagine that each HTTP Status Code has its own exception class? ......

小葫芦

This depends on how granular you are in feeding back error information to the calling interface party. For example, on a registration page, the user has many items to fill in. If you fill in the general information incorrectly, please fill it in again. This experience may not be very good. If you accurately say which item is filled in incorrectly, and also focus That fill-in item makes the experience much better.

迷茫

How many exception classes you use depends on how many different processing methods you want. In actual use, catching different exceptions will correspond to different operations. If one type of operation can be used to handle them, then the specific exception Just put the information in the message and bring it out.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!