Skip to content

Commit

Permalink
Refactor Exception class
Browse files Browse the repository at this point in the history
  • Loading branch information
Mahesh1772 committed Feb 22, 2024
1 parent 87b38bf commit f62e08a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/main/java/Kratos.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static void saveTasksToFile() {
}
}
} catch (IOException e) {
System.err.println("Error saving tasks to file: " + e.getMessage());
KratosException.handleException(e, "Error saving tasks to file: " + e.getMessage());
}
}

Expand All @@ -44,15 +44,15 @@ public static void loadTasksFromFile() {
}
} catch (FileNotFoundException e) {
// Handle the case where the file doesn't exist
System.err.println("File not found. Creating a new file...");
KratosException.handleException(e, "File not found. Creating a new file...");
File file = new File(FILE_PATH);
try {
file.createNewFile();
} catch (IOException ioException) {
System.err.println("Error creating a new file: " + ioException.getMessage());
KratosException.handleException(ioException, "Error creating a new file: " + ioException.getMessage());
}
} catch (IOException e) {
System.err.println("Error loading tasks from file: " + e.getMessage());
KratosException.handleException(e, "Error loading tasks from file: " + e.getMessage());
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/main/java/KratosException.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import java.io.FileNotFoundException;
import java.io.IOException;

public class KratosException extends Exception {
public static void handleException(Exception e, String command) {
if (e instanceof IllegalArgumentException) {
commandNotFound();
} else if (e instanceof ArrayIndexOutOfBoundsException) {
blankAfterTask(command);
} else if (e instanceof IOException) {
System.out.println(command);
} else if (e instanceof FileNotFoundException) {
System.out.println(command);
} else {
otherIncurredException();
}
Expand Down

0 comments on commit f62e08a

Please sign in to comment.