Is it a good or bad idea throwing Exceptions when validating data?

后端 未结 14 921
你的背包
你的背包 2020-12-07 16:27

When validating data, I\'ve gotten into a habit of doing the following:

Note: I don\'t really have individual booleans for each check. This is just

14条回答
  •  执笔经年
    2020-12-07 17:01

    This question is still interesting, mainly because of the answers.

    When it comes to exception, there is a lot of arguments involved. We can defend a point to any direction we want to, from performance to exception philosophy. And they all sounds right to me.

    But sometimes we have to stick to a direction. In this case, I think it's the validation itself.

    When we want to validate something we also want to know (to log, or to show the user) whats wrong when the parameter is invalid. Even thought there are layers of validation such as Business Validation mixed with User Input validations.

    For instance, when dealing with user input, a lot of weird cases can happen. A pasted data from a website full of hidden char (\t \n etc), typos, and a really huge kinds of cases that a specific exception could allow further analysis or message to the uses much more precisely than a simple "false" return.

提交回复
热议问题