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

后端 未结 17 1387
萌比男神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条回答
  •  Happy的楠姐
    2020-11-30 21:14

    A user entering 'bad' input is not an exception: it's to be expected.

    Exceptions should not be used for normal control flow.

    In the past many authors have said that Exceptions are inherently expensive. Jon Skeet has blogged contrary to this (and mentioned a few time in answers here on SO), saying that they are not as expensive as reported (although I wouldn’t advocate using them in a tight loop!)

    The biggest reason to use them is ‘statement of intent’ i.e. if you see an exception handling block you immediately see the exceptional cases which are dealt with outside of normal flow.

提交回复
热议问题