Write a program in java to demonstrate user define exception or custom exception?

class InvalidAgeException extends Exception

{

      public InvalidAgeException()

      {

            super("Age Range Is 18 To 60");

      }

}

public class CustomException

{

      public static void main(String arg[])

      {

            if(arg.length==0)

                  System.out.println("Enter Your Range As Command Argument");

            else

            {

                  try

                  {

                        int a=Integer.parseInt(arg[0]);

                        if(a<18 || a>60)

                              throw new InvalidAgeException();

                        System.out.println("Welcome To The Group Of Vote");

                  }

                  catch(Exception e)

                  {

                        System.out.println(e);

                  }

            }

      }

}

Custom Exception or User Define Exception.PNG

, , , , , ,

Leave a Reply