Write a Java program to input age from user and throw user-defined exception if entered age is negative.
import java.util.Scanner;
class AgeException extends Exception {
public AgeException(String str) {
System.out.println(str);
}
}
public class AgeExcDemo {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("Enter ur age :: ");
int age = s.nextInt();
try {
if(age < 18)
throw new AgeException("Invalid age");
else
throw new AgeException("Valid age");
}
catch (AgeException a) {
System.out.println(a);
}
}
}
Enter ur age :: 15
Invalid age
exception.AgeException
Enter ur age :: 20
Valid age
exception.AgeException