Should a business rule violation throw an exceptio

2019-03-12 21:16发布

Should a business rule violation throw an exception?

14条回答
男人必须洒脱
2楼-- · 2019-03-12 21:43

It depends on what the business rule is, IMO. I would venture to say "not usually" but I'd view it on a case-by-case basis. I don't think there is any one answer, as different business rules might warrant it while others might not.

查看更多
Summer. ? 凉城
3楼-- · 2019-03-12 21:43

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.

查看更多
登录 后发表回答