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.
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.