简体   繁体   中英

Java Checked vs Unchecked exception for validation service

I have a service that allow users to add dynamic content to a repository. So basically I have a generic Document class that contains a list of property for that specific object depending on what type of document the user is adding (eg. an invoice document has an invoice number property while a wiki document has an author property, and so on).

The service is comprised of different layers and at some point I have a class that has to check if the document to be added is compliant to the rules configurer, evaluating if all required properties are provided, if they are all of the right type, etc. If any of these validations fail I want to throw a custom exception which contains the validation status.

The question is: should my ValidationException be checked or unchecked? I read a lot of best practice on how to decide what kind of exception use. I was thinking about using RuntimeException but in this case the exception is not caused by error in coding or stuff like that but just by the user input... On the other hand using a checked exception would mean to propagate the "throws" syntax in all the above layer of the application and probably in the 90% of the service's methods making the code much less readable and manteinable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM