When to throw an exception?

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

    There are two main classes of exception:

    1) System exception (eg Database connection lost) or 2) User exception. (eg User input validation, 'password is incorrect')

    I found it helpful to create my own User Exception Class and when I want to throw a user error I want to be handled differently (ie resourced error displayed to the user) then all I need do in my main error handler is check the object type:

                If TypeName(ex) = "UserException" Then
                   Display(ex.message)
                Else
                   DisplayError("An unexpected error has occured, contact your help  desk")                   
                   LogError(ex)
                End If
    

提交回复
热议问题