Implementation of WS-05 is completed
This commit is contained in:
@@ -8,20 +8,47 @@ public class RuntimeExceptionDemo {
|
||||
UserEntity user = new UserEntity("Ali", "Ahmadi", 1234567890L,
|
||||
null, 989111111111L, null);
|
||||
|
||||
validateEmail(user);
|
||||
calculatePayment(5000, 0);
|
||||
convertUserAge("twenty");
|
||||
completeRegistration(user);
|
||||
// 1. Handle NullPointerException / IllegalArgumentException
|
||||
try {
|
||||
validateEmail(user);
|
||||
} catch (NullPointerException e) {
|
||||
System.err.println("Caught NullPointerException: Email string cannot be null.");
|
||||
} catch (IllegalArgumentException e) {
|
||||
System.err.println("Caught IllegalArgumentException: " + e.getMessage());
|
||||
}
|
||||
|
||||
// 2. Handle ArithmeticException
|
||||
try {
|
||||
calculatePayment(5000, 0);
|
||||
} catch (ArithmeticException e) {
|
||||
System.err.println("Caught ArithmeticException: Cannot divide payment by zero installments.");
|
||||
}
|
||||
|
||||
// 3. Handle NumberFormatException
|
||||
try {
|
||||
convertUserAge("twenty");
|
||||
} catch (NumberFormatException e) {
|
||||
System.err.println("Caught NumberFormatException: Could not parse invalid age string.");
|
||||
}
|
||||
|
||||
// 4. Handle IllegalStateException
|
||||
try {
|
||||
completeRegistration(user);
|
||||
} catch (IllegalStateException e) {
|
||||
System.err.println("Caught IllegalStateException: " + e.getMessage());
|
||||
}
|
||||
|
||||
System.out.println("\nRuntimeExceptionDemo executed completely and safely!");
|
||||
}
|
||||
|
||||
|
||||
//nullPointerException
|
||||
//IllegalArgumentException
|
||||
public static void validateEmail(UserEntity user) {
|
||||
|
||||
String email = user.getEmail();
|
||||
|
||||
// Fix to prevent immediate NullPointerException before regex matching
|
||||
if (email == null) {
|
||||
throw new NullPointerException("Email cannot be null");
|
||||
}
|
||||
|
||||
String emailRegex = "^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+$";
|
||||
|
||||
if (!Pattern.matches(emailRegex, email)) {
|
||||
@@ -31,32 +58,22 @@ public class RuntimeExceptionDemo {
|
||||
System.out.println("User email is valid");
|
||||
}
|
||||
|
||||
// ArithmeticException
|
||||
public static void calculatePayment(int totalAmount, int installmentCount) {
|
||||
|
||||
int payment = totalAmount / installmentCount;
|
||||
|
||||
System.out.println("Each installment : " + payment);
|
||||
}
|
||||
|
||||
// NumberFormatException
|
||||
public static void convertUserAge(String ageInput) {
|
||||
|
||||
int age = Integer.parseInt(ageInput);
|
||||
|
||||
System.out.println("User age: " + age);
|
||||
}
|
||||
|
||||
// IllegalStateException
|
||||
public static void completeRegistration(UserEntity user) {
|
||||
|
||||
if (user.getDateOfBirth() == null) {
|
||||
throw new IllegalStateException(
|
||||
"Registration cannot be completed. Date of birth is missing."
|
||||
);
|
||||
}
|
||||
|
||||
System.out.println("Registration completed");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user