Why are Exceptions said to be so bad for Input Validation?

后端 未结 17 1370
萌比男神i
萌比男神i 2020-11-30 20:37

I understand that \"Exceptions are for exceptional cases\" [a], but besides just being repeated over and over again, I\'ve never found an actual reason for this fact.

<
17条回答
  •  独厮守ぢ
    2020-11-30 21:17

    This is a linguistic pov( point of view) on the matter.

    Why are Exceptions said to be so bad for Input Validation?

    conclusion :

    • Exceptions are not defined clearly enough, so there are different opinions.
    • Wrong input is seen as a normal thing, not as an exception.

    thoughts ?

    It probably comes down to the expectations one takes about the code that is created.

    • the client can not be trusted
      • validation has to happen at the server's side. stronger : every validation happens at server's side.
      • because validation happens at the server's side it is expected to be done there and what is expected is not an exception, since it is expected.

    However,

    • the client's input can not to be trusted
    • the client's input-validation can be trusted
      • if validation is trusted it can be expected to produce valid input
      • now every input is expected to be valid
      • invalid input is now unexpected, an exception

    .

    exceptions can be a nice way to exit the code.

    A thing mentioned to consider is if your code is left in a proper state. I would not know what would leave my code in an improper state. Connections get closed automatically, leftover variables are garbage-collected, what's the problem?

提交回复
热议问题