Try / Catch in Constructor - Recommended Practice?

后端 未结 3 973
我在风中等你
我在风中等你 2020-12-05 10:06

Something I\'ve always been curious of

public class FileDataValidator {

private String[] lineData;

public FileDataValidator(String[] lineData){

    this.l         


        
3条回答
  •  青春惊慌失措
    2020-12-05 10:44

    My preference is for exceptions to be dealt with by the bit of code that knows how to deal with them. In this case I would assume that the bit of code creating a FileDataValidator knows what should happen if the file data is not valid, and the exceptions should be dealt with there (I am advocating propagating to the caller).

    Whilst discussing best practice - the class name FileDataValidator smells to me. If the object you're creating stores file data then I would call it FileData - perhaps with a validate method? If you only want to validate your file data then a static method would suffice.

提交回复
热议问题