Issue
For example, here is my code
/**
* Generates a message and saves it to a file where placeholders '%s' are replaced with values
* provided during runtime from properties file.
*
* @param formattedMessage the formatted message where placeholders are replaced to '%s'
* @param placeholderKeys list with placeholder keys
* @return string
*/
public String generateMessage(String formattedMessage, List<String> placeholderKeys,
String inputFilePath, String outputFilePath) {
pathValidator.throwExceptionIfInputPathInvalid(inputFilePath);
pathValidator.throwExceptionIfOutputPathInvalid(outputFilePath);
List<String> placeholderValues = fileUtil.getPlaceholderValues(inputFilePath, placeholderKeys);
String generatedMessage = String.format(formattedMessage, placeholderValues.toArray());
fileUtil.writeToFile(outputFilePath, generatedMessage);
return generatedMessage;
}
As you can see, I have two method calls the only meaning of which is to throw exceptions. Here is one of them:
/**
* Throws exceptions if input file path is invalid
*
* @throws InvalidInputException if input is blank
* @throws InvalidFileExtensionException if provided file is not of a properties type
* @throws PathNotValidException if provided output file path is not valid
* @throws FileNotFoundException if provided input file is not found
*/
public void throwExceptionIfInputPathInvalid(String path) {
throwExceptionIfOutputPathInvalid(path);
if (!InputValidator.isExtensionValid(new File(path), ".properties"))
throw new InvalidFileExtensionException("File should be of properties type");
if (!Files.exists(Paths.get(path))) throw new FileNotFoundException();
}
Do I need to include these exceptions to docs of a generateMessage
method?
Solution
I'd include these exceptions. Someone using the method generateMessage
shouldn't have to know (or care) that internally it uses throwExceptionIfInputPathInvalid
.
Alternatively, you could say something like "This method uses throwExceptionIfInputPathInvalid
to check the validity of inputFilePath
and outputFilePath
. See its documentation for details on the exceptions it may throw."
Answered By - Mureinik
Answer Checked By - Senaida (JavaFixing Volunteer)