When to throw an exception?

后端 未结 30 2426
后悔当初
后悔当初 2020-11-21 23:48

I have exceptions created for every condition that my application does not expect. UserNameNotValidException, PasswordNotCorrectException etc.

30条回答
  •  轮回少年
    2020-11-22 00:42

    My little guidelines are heavily influenced by the great book "Code complete":

    • Use exceptions to notify about things that should not be ignored.
    • Don't use exceptions if the error can be handled locally
    • Make sure the exceptions are at the same level of abstraction as the rest of your routine.
    • Exceptions should be reserved for what's truly exceptional.

提交回复
热议问题