exception handling and basic file operations in Java
This commit is contained in:
@@ -1,5 +1,11 @@
|
|||||||
package org.Exceptions;
|
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.text.SimpleDateFormat;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
@@ -45,35 +51,32 @@ public class FileService {
|
|||||||
|
|
||||||
|
|
||||||
public static String writeFile(UserEntity user) {
|
public static String writeFile(UserEntity user) {
|
||||||
//TODO: Uncomment the lines below.
|
try {
|
||||||
//TODO: Use your knowledge of exception handling to make the code work as expected
|
FileWriter fw = new FileWriter("userInformation.txt", true);
|
||||||
|
BufferedWriter bw = new BufferedWriter(fw);
|
||||||
|
|
||||||
/* Uncomment this section
|
bw.write(user.toString());
|
||||||
//TODO: handle the exception
|
bw.close();
|
||||||
FileWriter fw = new FileWriter("userInformation.txt", true);
|
} catch (IOException e) {
|
||||||
BufferedWriter bw = new BufferedWriter(fw);
|
System.out.println("Error writing to file: " + e.getMessage());
|
||||||
|
return "failed to write user information";
|
||||||
bw.write(user.toString());
|
}
|
||||||
bw.close();
|
|
||||||
*/
|
|
||||||
return "user information written to file";
|
return "user information written to file";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static void readFile() {
|
public static void readFile() {
|
||||||
//TODO: Uncomment the lines below.
|
try {
|
||||||
//TODO: Use your knowledge of exception handling to make the code work as expected
|
FileReader file = new FileReader("userInformation.txt");
|
||||||
|
BufferedReader fileInput = new BufferedReader(file);
|
||||||
|
|
||||||
/* Uncomment this section
|
for (int counter = 0; counter < 3; counter++)
|
||||||
FileReader file = new FileReader("userInformation.txt");
|
System.out.println(fileInput.readLine());
|
||||||
|
|
||||||
BufferedReader fileInput = new BufferedReader(file);
|
fileInput.close();
|
||||||
|
} catch (IOException e) {
|
||||||
for (int counter = 0; counter < 3; counter++)
|
System.out.println("Error reading file: " + e.getMessage());
|
||||||
System.out.println(fileInput.readLine());
|
}
|
||||||
|
|
||||||
fileInput.close();
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -82,9 +85,11 @@ public class FileService {
|
|||||||
|
|
||||||
Date dob = null;
|
Date dob = null;
|
||||||
|
|
||||||
//TODO: Uncomment this line.
|
try {
|
||||||
//TODO: Use your knowledge of exception handling to make the code work as expected
|
dob = formatter.parse(date);
|
||||||
//dob = formatter.parse(date);
|
} catch (ParseException e) {
|
||||||
|
System.out.println("Error: invalid date format, expected yyyy-MM-dd - " + e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
return dob;
|
return dob;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,44 +19,58 @@ public class RuntimeExceptionDemo {
|
|||||||
//nullPointerException
|
//nullPointerException
|
||||||
//IllegalArgumentException
|
//IllegalArgumentException
|
||||||
public static void validateEmail(UserEntity user) {
|
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)) {
|
System.out.println("User email is valid");
|
||||||
throw new IllegalArgumentException("Invalid email format");
|
} 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
|
// ArithmeticException
|
||||||
public static void calculatePayment(int totalAmount, int installmentCount) {
|
public static void calculatePayment(int totalAmount, int installmentCount) {
|
||||||
|
try {
|
||||||
|
int payment = totalAmount / installmentCount;
|
||||||
|
|
||||||
int payment = totalAmount / installmentCount;
|
System.out.println("Each installment : " + payment);
|
||||||
|
} catch (ArithmeticException e) {
|
||||||
System.out.println("Each installment : " + payment);
|
System.out.println("Error: cannot divide by zero - " + e.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NumberFormatException
|
// NumberFormatException
|
||||||
public static void convertUserAge(String ageInput) {
|
public static void convertUserAge(String ageInput) {
|
||||||
|
try {
|
||||||
|
int age = Integer.parseInt(ageInput);
|
||||||
|
|
||||||
int age = Integer.parseInt(ageInput);
|
System.out.println("User age: " + age);
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
System.out.println("User age: " + age);
|
System.out.println("Error: invalid number format - " + e.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// IllegalStateException
|
// IllegalStateException
|
||||||
public static void completeRegistration(UserEntity user) {
|
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) {
|
System.out.println("Registration completed");
|
||||||
throw new IllegalStateException(
|
} catch (IllegalStateException e) {
|
||||||
"Registration cannot be completed. Date of birth is missing."
|
System.out.println("Error: " + e.getMessage());
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.println("Registration completed");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user