I think the following code stops if it encounters any error, period.
public static void exportDatapointsToCSV(List<Datapoint> datapointList, File exportDir) {
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy (h:mm a)", Locale.US);
File file = new File(exportDir, "my_datapoints.csv");
try {
file.createNewFile();
CSVWriter csvWrite = new CSVWriter(new FileWriter(file));
csvWrite.writeNext(new String[]{"DATAPOINT_LABEL", "VALUE", "TIMESTAMP", "TIMESTAMP_FORMATTED", "NOTE"});
for (Datapoint datapoint : datapointList) {
String arrStr[] = new String[]{
datapoint.getLabel(),
datapoint.getValue() + "",
datapoint.getTimestamp() + "",
sdf.format(new Date(datapoint.getTimestamp())) + "",
datapoint.getNote()
};
csvWrite.writeNext(arrStr);
}
csvWrite.close();
}
catch (Exception sqlEx) {
Log.e(LOG_TAG, sqlEx.getMessage(), sqlEx);
}
}
But I want it to:
Throw an error and stop if it can't create the file to begin with.
If it encounters an error on a given row, log it but move onto the next row, rather than stopping the entire thing.
Do I just move the try/catch into the loop? Does that cover it?