When to throw an exception?

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

    I have three type of conditions that I catch.

    1. Bad or missing input should not be an exception. Use both client side js and server side regex to detect, set attributes and forward back to the same page with messages.

    2. The AppException. This is usually an exception that you detect and throw with in your code. In other words these are ones you expect (the file does not exist). Log it, set the message, and forward back to the general error page. This page usually has a bit of info about what happened.

    3. The unexpected Exception. These are the ones you don't know about. Log it with details and forward them to a general error page.

    Hope this helps

提交回复
热议问题