Business Objects, Validation And Exceptions

后端 未结 18 923
自闭症患者
自闭症患者 2020-12-02 04:54

I’ve been reading a few questions and answers regarding exceptions and their use. Seems to be a strong opinion that exceptions should be raised only for exception, unhandled

18条回答
  •  没有蜡笔的小新
    2020-12-02 05:31

    Throwing an exception in your case is fine. You can consider the case a true exception because something is trying to set an integer to a string (for example). The business rules lack of knowledege of your views means that they should consider this case exceptonal and return that back to the view.

    Whether or not you validate your input values before you send them through to the business layer is up to you, I think that as long as you follow the same standard throughout your application then you will end up with clean and readable code.

    You could use the spring framework as specified above, just be careful as much of the linked document was indicating writing code that is not strongly typed, I.E. you may get errors at run time that you could not pick up at compile time. This is something I try to avoid as much as possible.

    The way we do it here currently is that we take all the input values from the screen, bind them to a data model object and throw an exception if a value is in error.

提交回复
热议问题