try {
addressBookOptional = storage.readAddressBook();
if (!addressBookOptional.isPresent()) {
logger.info("Data file not found. Will be starting with a sample AddressBook");
}
initialData = addressBookOptional.orElseGet(SampleDataUtil::getSampleAddressBook);
} catch (DataConversionException e) {
logger.warning("Data file not in the correct format. Will be starting with an empty AddressBook");
initialData = new AddressBook();
} catch (IOException e) {
logger.warning("Problem while reading from the file. Will be starting with an empty AddressBook");
initialData = new AddressBook();
}
try {
Optional<UserPrefs> prefsOptional = storage.readUserPrefs();
initializedPrefs = prefsOptional.orElse(new UserPrefs());
} catch (DataConversionException e) {
logger.warning("UserPrefs file at " + prefsFilePath + " is not in the correct format. "
+ "Using default user prefs");
initializedPrefs = new UserPrefs();
} catch (IOException e) {
logger.warning("Problem while reading from the file. Will be starting with an empty AddressBook");
initializedPrefs = new UserPrefs();
}
The IOException was due to readUserPrefs() and readAddressBook() possibly throwing IOException suggested by their respective interface files.
However, in the actual implementation of the methods, the IOException is actually already handled within the functions itself so this means that IOException is actually never thrown. Thus, I believe it is safe to remove the catch statement for IOException as well as in the interface.
@Override
public Optional<UserPrefs> readUserPrefs() throws DataConversionException {
return readUserPrefs(filePath);
}
/**
* Similar to {@link #readUserPrefs()}
* @param prefsFilePath location of the data. Cannot be null.
* @throws DataConversionException if the file format is not as expected.
*/
public Optional<UserPrefs> readUserPrefs(Path prefsFilePath) throws DataConversionException {
return JsonUtil.readJsonFile(prefsFilePath, UserPrefs.class);
}
In MainApp.java,
The IOException was due to
readUserPrefs()
andreadAddressBook()
possibly throwingIOException
suggested by their respective interface files.However, in the actual implementation of the methods, the
IOException
is actually already handled within the functions itself so this means thatIOException
is actually never thrown. Thus, I believe it is safe to remove the catch statement forIOException
as well as in the interface.e.g.
readUserPref()
In StorageManager.java
In JsonUserPrefStorage.java
In JsonUtil.java
readAddressBook()
in StorageManager.java
In JsonAddressBookStorage.java
We can see that none of the classes that implemented those interface actually throws
IOExceptions