feat(exceptions): implement examples of checked and unchecked exception handling

This commit is contained in:
2026-05-10 00:07:12 +03:30
parent f29dc564fb
commit 286b7aaef3
5 changed files with 239 additions and 0 deletions
@@ -0,0 +1,86 @@
package org.exceptions;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class FileService {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter you first name: ");
String firstName = scanner.nextLine();
System.out.println("Enter you last name: ");
String lastName = scanner.nextLine();
System.out.println("Enter your national code: ");
Long nationalCode = scanner.nextLong();
scanner.nextLine();
System.out.println("Enter your email: ");
String email = scanner.nextLine();
System.out.println("Enter your phone number: ");
Long phoneNumber = scanner.nextLong();
scanner.nextLine();
System.out.println("Enter your dateOfBirth: ");
String dateOfBirth = scanner.nextLine();
UserEntity user = null;
user = new UserEntity(firstName, lastName, nationalCode, email, phoneNumber, parseDate(dateOfBirth));
System.out.println(user.toString());
System.out.println(writeFile(user));
readFile();
}
public static String writeFile(UserEntity user) {
FileWriter fw = new FileWriter("D:\\ExceptionHandling\\userInformation.txt", true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(user.toString());
bw.close();
return "user information written to file";
}
public static void readFile() {
FileReader file = new FileReader("D:\\ExceptionHandling\\userInformation.txt");
BufferedReader fileInput = new BufferedReader(file);
for (int counter = 0; counter < 3; counter++)
System.out.println(fileInput.readLine());
fileInput.close();
}
public static Date parseDate(String date) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date dob = null;
dob = formatter.parse(date);
return dob;
}
}