Skip to main content
Corrected typos and grammar.
Source Link
Adam Zuckerman
  • 3.7k
  • 1
  • 21
  • 27

OfcourseOf course this depends on the implementation and the degree of complexity of your system. But generally speaking, iI would opt for creating something like a FileParsingExceptionFileParsingException and an ObjectBuildingExceptionObjectBuildingException.

Then you would probably make an ObjectReaderObjectReader class that parses the file and creates the object (potentially delegated to a FileParserFileParser and and an ObjectBuilderObjectBuilder class), catches these exceptions and creates the correct exception. Then

Then you can catch the exception at some point in the stack where you can give proper user feedback and maybe give the option of selecting amotheranother file or whatnot.

Good thing is that you can then add relevant information of what went wrong without enforcing complexity on the user of the class.

You for example might not want the client class to know that the file must contain intsints because that would require it to implement logic that will break when your reader class changes. If there are a lot of clients, this becomes a hell very quickly. (this propablyprobably makes more sencesense when the parsing is more complicated).

Ofcourse this depends on the implementation and the degree of complexity of your system. But generally speaking, i would opt for creating something like a FileParsingException and an ObjectBuildingException.

Then you would probably make an ObjectReader class that parses the file and creates the object (potentially delegated to a FileParser and and an ObjectBuilder class), catches these exceptions and creates the correct exception. Then you can catch the exception at some point in the stack where you can give proper user feedback and maybe give the option of selecting amother file or whatnot.

Good thing is that you can then add relevant information of what went wrong without enforcing complexity on the user of the class.

You for example might not want the client class to know that the file must contain ints because that would require it to implement logic that will break when your reader class changes. If there are a lot of clients this becomes a hell very quickly. (this propably makes more sence when the parsing is more complicated).

Of course this depends on the implementation and the degree of complexity of your system. But generally speaking, I would opt for creating something like a FileParsingException and an ObjectBuildingException.

Then you would probably make an ObjectReader class that parses the file and creates the object (potentially delegated to a FileParser and and an ObjectBuilder class), catches these exceptions and creates the correct exception.

Then you can catch the exception at some point in the stack where you can give proper user feedback and maybe give the option of selecting another file or whatnot.

Good thing is that you can then add relevant information of what went wrong without enforcing complexity on the user of the class.

You for example might not want the client class to know that the file must contain ints because that would require it to implement logic that will break when your reader class changes. If there are a lot of clients, this becomes a hell very quickly. (this probably makes more sense when the parsing is more complicated).

Source Link
Teimpz
  • 246
  • 1
  • 4

Ofcourse this depends on the implementation and the degree of complexity of your system. But generally speaking, i would opt for creating something like a FileParsingException and an ObjectBuildingException.

Then you would probably make an ObjectReader class that parses the file and creates the object (potentially delegated to a FileParser and and an ObjectBuilder class), catches these exceptions and creates the correct exception. Then you can catch the exception at some point in the stack where you can give proper user feedback and maybe give the option of selecting amother file or whatnot.

Good thing is that you can then add relevant information of what went wrong without enforcing complexity on the user of the class.

You for example might not want the client class to know that the file must contain ints because that would require it to implement logic that will break when your reader class changes. If there are a lot of clients this becomes a hell very quickly. (this propably makes more sence when the parsing is more complicated).