When to throw an exception?

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

    the main reason for avoiding throwing an exception is that there is a lot of overhead involved with throwing an exception.

    One thing the article below states is that an exception is for an exceptional conditions and errors.

    A wrong user name is not necessarily a program error but a user error...

    Here is a decent starting point for exceptions within .NET: http://msdn.microsoft.com/en-us/library/ms229030(VS.80).aspx

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-22 00:33

    "PasswordNotCorrectException" isn't a good example for using exceptions. Users getting their passwords wrong is to be expected, so it's hardly an exception IMHO. You probably even recover from it, showing a nice error message, so it's just a validity check.

    Unhandled exceptions will stop the execution eventually - which is good. If you're returning false, null or error codes, you will have to deal with the program's state all by yourself. If you forget to check conditions somewhere, your program may keep running with wrong data, and you may have a hard time figuring out what happened and where.

    Of course, you could cause the same problem with empty catch statements, but at least spotting those is easier and doesn't require you to understand the logic.

    So as a rule of thumb:

    Use them wherever you don't want or simply can't recover from an error.

    0 讨论(0)
  • 2020-11-22 00:34

    Exceptions are intended for events that are abnormal behaviors, errors, failures, and such. Functional behavior, user error, etc., should be handled by program logic instead. Since a bad account or password is an expected part of the logic flow in a login routine, it should be able to handle those situations without exceptions.

    0 讨论(0)
  • 2020-11-22 00:35

    Because they're things that will happen normally. Exceptions are not control flow mechanisms. Users often get passwords wrong, it's not an exceptional case. Exceptions should be a truly rare thing, UserHasDiedAtKeyboard type situations.

    0 讨论(0)
  • 2020-11-22 00:35

    Security is conflated with your example: You shouldn't tell an attacker that a username exists, but the password is wrong. That's extra information you don't need to share. Just say "the username or password is incorrect."

    0 讨论(0)
提交回复
热议问题