From f62e08a02e865a61947dbb76b4f6773adab59c94 Mon Sep 17 00:00:00 2001 From: Mahesh1772 <87927591+Mahesh1772@users.noreply.github.com> Date: Thu, 22 Feb 2024 16:42:36 +0800 Subject: [PATCH] Refactor Exception class --- src/main/java/Kratos.java | 8 ++++---- src/main/java/KratosException.java | 7 +++++++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/main/java/Kratos.java b/src/main/java/Kratos.java index c1ea01d30..50fed1aff 100644 --- a/src/main/java/Kratos.java +++ b/src/main/java/Kratos.java @@ -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()); } } @@ -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()); } } diff --git a/src/main/java/KratosException.java b/src/main/java/KratosException.java index ee7624b85..a60681b40 100644 --- a/src/main/java/KratosException.java +++ b/src/main/java/KratosException.java @@ -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(); }