Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
08754f98f4 |
@@ -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
|
||||
|
||||
/* Uncomment this section
|
||||
//TODO: handle the exception
|
||||
try {
|
||||
FileWriter fw = new FileWriter("userInformation.txt", true);
|
||||
BufferedWriter bw = new BufferedWriter(fw);
|
||||
|
||||
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
|
||||
|
||||
/* Uncomment this section
|
||||
try {
|
||||
FileReader file = new FileReader("userInformation.txt");
|
||||
|
||||
BufferedReader fileInput = new BufferedReader(file);
|
||||
|
||||
for (int counter = 0; counter < 3; counter++)
|
||||
System.out.println(fileInput.readLine());
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ public class RuntimeExceptionDemo {
|
||||
//nullPointerException
|
||||
//IllegalArgumentException
|
||||
public static void validateEmail(UserEntity user) {
|
||||
|
||||
try {
|
||||
String email = user.getEmail();
|
||||
|
||||
String emailRegex = "^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+$";
|
||||
@@ -29,27 +29,38 @@ public class RuntimeExceptionDemo {
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
// ArithmeticException
|
||||
public static void calculatePayment(int totalAmount, int installmentCount) {
|
||||
|
||||
try {
|
||||
int payment = totalAmount / installmentCount;
|
||||
|
||||
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);
|
||||
|
||||
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."
|
||||
@@ -57,6 +68,9 @@ public class RuntimeExceptionDemo {
|
||||
}
|
||||
|
||||
System.out.println("Registration completed");
|
||||
} catch (IllegalStateException e) {
|
||||
System.out.println("Error: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user