Skip to content

Commit

Permalink
Add code to handle errors caused by loading tasks from storage
Browse files Browse the repository at this point in the history
  • Loading branch information
xzynos committed Sep 25, 2022
1 parent 981476b commit 9b4301e
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/main/java/duke/storage/LocalStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.nio.file.StandardOpenOption;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;

@SuppressWarnings({"PatternVariableCanBeUsed", "StringConcatenationInLoop"})
Expand Down Expand Up @@ -200,7 +201,6 @@ public static void saveTasks(ArrayList<Task> tasks, String path, String filename
}

Files.writeString(tasksFilePath, tasksStr, StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);

}

/**
Expand All @@ -220,7 +220,15 @@ public static ArrayList<Task> loadTasks(String path, String filename) throws IOE

String[] tasksStrArr = tasksStr.split("\n");
for (String taskStr : tasksStrArr) {
Task task = convertFromStorageSafeFormat(taskStr);
Task task;

try {
task = convertFromStorageSafeFormat(taskStr);
} catch (IndexOutOfBoundsException |
DateTimeParseException e) {
task = null;
}

if (task != null) {
tasks.add(task);
}
Expand Down

0 comments on commit 9b4301e

Please sign in to comment.