Tuesday, 4 March 2025

Write a java program to accept Employee name from the user and check whether it is valid or not. If it is not valid then throw user defined Exception “Name is Invalid” otherwise display it.



import java.util.Scanner;


class InvalidNameException extends Exception

{

    InvalidNameException(String msg)

    {

        super(msg);

    }

}


class EmployeeName

{

    public static void main(String args[])

    {

        Scanner sc = new Scanner(System.in);


        try

        {

            System.out.print("Enter Employee Name: ");

            String name = sc.nextLine();


                 if(name.trim().isEmpty())

                throw new InvalidNameException("Name is Invalid");


            System.out.println("Employee Name: " + name);

        }

        catch(Exception e)

        {

            System.out.println(e.getMessage());

        }


        sc.close();

    }

}