This commit is contained in:
2026-05-15 17:48:54 +03:30
parent 015840161b
commit eedbbc2bc9
4 changed files with 4 additions and 8 deletions
@@ -0,0 +1,62 @@
package org.Exceptions;
import java.util.regex.Pattern;
public class RuntimeExceptionDemo {
public static void main(String[] args) {
UserEntity user = new UserEntity("Ali", "Ahmadi", 1234567890L,
null, 989111111111L, null);
validateEmail(user);
calculatePayment(5000, 0);
convertUserAge("twenty");
completeRegistration(user);
}
//nullPointerException
//IllegalArgumentException
public static void validateEmail(UserEntity user) {
String email = user.getEmail();
String emailRegex = "^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+$";
if (!Pattern.matches(emailRegex, email)) {
throw new IllegalArgumentException("Invalid email format");
}
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");
}
}