Write a JAVA program for creation of user defined exception.
Write a JAVA program for creation of user defined exception.
import java.util.Scanner;
class InvalidAgeException extends Exception
{
InvalidAgeException(String s)
{
super(s);
}
}
class TestCustomException
{
static void validate(int age)throws InvalidAgeException
{
if(age<18)
throw new InvalidAgeException("You Are not eligible to vote");
else
System.out.println("You are Eligible, welcome to vote");
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Age");
int a=sc.nextInt();
try
{
validate(a);
}
catch(Exception m)
{
System.out.println(" Exception occured: "+m);
}
}
}
Labels: Java Programming Lab
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home