When to throw an exception?

后端 未结 30 1992
后悔当初
后悔当初 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:33

    In general you want to throw an exception for anything that can happen in your application that is "Exceptional"

    In your example, both of those exceptions look like you are calling them via a password / username validation. In that case it can be argued that it isn't really exceptional that someone would mistype a username / password.

    They are "exceptions" to the main flow of your UML but are more "branches" in the processing.

    If you attempted to access your passwd file or database and couldn't, that would be an exceptional case and would warrant throwing an exception.

提交回复
热议问题