Should a business rule violation throw an exception?

后端 未结 14 696
误落风尘
误落风尘 2021-02-04 04:18

Should a business rule violation throw an exception?

14条回答
  •  灰色年华
    2021-02-04 05:10

    Do you mean, for example, that a value is supposed to be in the range 0-99 but somehow ended up being 105?

    If it's coming from the user it's a matter of validation. Whether it is handled using exceptions or not depends on the idioms of your language.

    If it's coming from your data store then yes, it seems reasonable to throw an exception. It means you have bad data and you need to figure out how it got there and prevent it from happening again.

提交回复
热议问题