Tuesday, 4 March 2025

Write a java program to check whether given candidate is eligible for voting or not. Handle user defined as well as system definedException.

  

 

import java.util.Scanner;

class InvalidAgeException extends Exception

{

    InvalidAgeException(String msg)

    {

        super(msg);

    }

}


class Voting

{

    public static void main(String args[])

    {

        Scanner sc = new Scanner(System.in);


        try

        {

            System.out.print("Enter age: ");

            int age = sc.nextInt();   


            if(age < 18)

            {

                throw new InvalidAgeException("Not eligible for voting");

            }


            System.out.println("Eligible for voting");

        }

        catch(Exception e)   

        {

            System.out.println("Exception: " + e.getMessage());

        }


        sc.close();

    }

}