diff --git a/src/main/java/org/Exceptions/FileService.java b/src/main/java/org/Exceptions/FileService.java index 3aa276f..60e6dd0 100644 --- a/src/main/java/org/Exceptions/FileService.java +++ b/src/main/java/org/Exceptions/FileService.java @@ -1,5 +1,11 @@ package org.Exceptions; +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Scanner; @@ -45,35 +51,32 @@ public class FileService { public static String writeFile(UserEntity user) { - //TODO: Uncomment the lines below. - //TODO: Use your knowledge of exception handling to make the code work as expected + try { + FileWriter fw = new FileWriter("userInformation.txt", true); + BufferedWriter bw = new BufferedWriter(fw); - /* Uncomment this section - //TODO: handle the exception - FileWriter fw = new FileWriter("userInformation.txt", true); - BufferedWriter bw = new BufferedWriter(fw); - - bw.write(user.toString()); - bw.close(); - */ + bw.write(user.toString()); + bw.close(); + } catch (IOException e) { + System.out.println("Error writing to file: " + e.getMessage()); + return "failed to write user information"; + } return "user information written to file"; } public static void readFile() { - //TODO: Uncomment the lines below. - //TODO: Use your knowledge of exception handling to make the code work as expected + try { + FileReader file = new FileReader("userInformation.txt"); + BufferedReader fileInput = new BufferedReader(file); - /* Uncomment this section - FileReader file = new FileReader("userInformation.txt"); + for (int counter = 0; counter < 3; counter++) + System.out.println(fileInput.readLine()); - BufferedReader fileInput = new BufferedReader(file); - - for (int counter = 0; counter < 3; counter++) - System.out.println(fileInput.readLine()); - - fileInput.close(); - */ + fileInput.close(); + } catch (IOException e) { + System.out.println("Error reading file: " + e.getMessage()); + } } @@ -82,9 +85,11 @@ public class FileService { Date dob = null; - //TODO: Uncomment this line. - //TODO: Use your knowledge of exception handling to make the code work as expected - //dob = formatter.parse(date); + try { + dob = formatter.parse(date); + } catch (ParseException e) { + System.out.println("Error: invalid date format, expected yyyy-MM-dd - " + e.getMessage()); + } return dob; } diff --git a/src/main/java/org/Exceptions/RuntimeExceptionDemo.java b/src/main/java/org/Exceptions/RuntimeExceptionDemo.java index 3d57b69..e180f86 100644 --- a/src/main/java/org/Exceptions/RuntimeExceptionDemo.java +++ b/src/main/java/org/Exceptions/RuntimeExceptionDemo.java @@ -19,44 +19,58 @@ public class RuntimeExceptionDemo { //nullPointerException //IllegalArgumentException public static void validateEmail(UserEntity user) { + try { + String email = user.getEmail(); - String email = user.getEmail(); + String emailRegex = "^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+$"; - String emailRegex = "^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+$"; + if (!Pattern.matches(emailRegex, email)) { + throw new IllegalArgumentException("Invalid email format"); + } - if (!Pattern.matches(emailRegex, email)) { - throw new IllegalArgumentException("Invalid email format"); + System.out.println("User email is valid"); + } catch (NullPointerException e) { + System.out.println("Error: email is null - " + e.getMessage()); + } catch (IllegalArgumentException e) { + System.out.println("Error: " + e.getMessage()); } - - System.out.println("User email is valid"); } // ArithmeticException public static void calculatePayment(int totalAmount, int installmentCount) { + try { + int payment = totalAmount / installmentCount; - int payment = totalAmount / installmentCount; - - System.out.println("Each installment : " + payment); + System.out.println("Each installment : " + payment); + } catch (ArithmeticException e) { + System.out.println("Error: cannot divide by zero - " + e.getMessage()); + } } // NumberFormatException public static void convertUserAge(String ageInput) { + try { + int age = Integer.parseInt(ageInput); - int age = Integer.parseInt(ageInput); - - System.out.println("User age: " + age); + System.out.println("User age: " + age); + } catch (NumberFormatException e) { + System.out.println("Error: invalid number format - " + e.getMessage()); + } } // IllegalStateException public static void completeRegistration(UserEntity user) { + try { + if (user.getDateOfBirth() == null) { + throw new IllegalStateException( + "Registration cannot be completed. Date of birth is missing." + ); + } - if (user.getDateOfBirth() == null) { - throw new IllegalStateException( - "Registration cannot be completed. Date of birth is missing." - ); + System.out.println("Registration completed"); + } catch (IllegalStateException e) { + System.out.println("Error: " + e.getMessage()); } - - System.out.println("Registration completed"); } } \ No newline at end of file