Ques WAP for exception handling to check user age is eligible to vote or not in java?
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 Your Age :: ");
int age = s.nextInt();
try {
if(age < 18)
throw new AgeException("You Are Not Eligible To Vote");
else
System.out.println("You Are Eligible To Vote");
}
catch (AgeException a) {
System.out.println(a);
}
}
}
//Output :
Comments
Post a Comment