search

Home  >  Q&A  >  body text

java - 项目中自定义异常用一个还是多个?

高洛峰高洛峰2814 days ago976

reply all(4)I'll reply

  • PHP中文网

    PHP中文网2017-04-17 13:09:48

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

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 13:09:48

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

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 13:09:48

    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.

    reply
    0
  • 迷茫

    迷茫2017-04-17 13:09:48

    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.

    reply
    0
  • Cancelreply