Commit bb573060 authored by Bhargavi Ghanta's avatar Bhargavi Ghanta

Updated AgeValidator and ExceptionClass with latest changes

parent a691cd74
public class AgeValidator {
// Method to validate age
public static void validateAge(int age) {
try {
if (age <= 0) {
throw new InvalidAgeException("Age must be greater than 0. Invalid input: " + age);
} else {
System.out.println("Valid age: " + age);
}
} catch (InvalidAgeException e) {
throw e; // Rethrow so it can be caught in main
} catch (Exception e) {
System.out.println("Unexpected error in validateAge: " + e.getMessage());
}
}
// Main Method
public static void main(String[] args) {
int[] testAges = {95, -67, -20, -12, -32 , -58, 98, 12, 3 , 15}; // Test cases
for (int age : testAges) {
try {
validateAge(age);
} catch (InvalidAgeException iae) {
System.out.println("Caught InvalidAgeException: " + iae.getMessage());
} catch (Exception e) {
System.out.println("Caught General Exception: " + e.getMessage());
}
}
}
}
\ No newline at end of file
// Custom Exception Class
class InvalidAgeException extends RuntimeException {
public InvalidAgeException(String message) {
super(message);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment