Java Custom Exception - User Defined Exception

Advertisements

Java User Defined Exception

In Java, we can create our own exception class specific to our application or program. Such kinds of exceptions are known as user defined exception or custom exception. To create our own exception class, we must inherit exception class so that our exception class become the part of exception hierarchy.

Example 1 of user defined OddNumerException class


//UserdefinedExceptionDemo

    import java.util.Scanner;

    class OddNumberException extends Exception  // Statement 1
    {
        OddNumberException()
        {
            super("Odd number exception");
        }
        OddNumberException(String msg)
        {
            super(msg);
        }
    }

    class UserdefinedExceptionDemo
    {
        public static void main(String[] args)
        {
            int num;

            Scanner Sc = new Scanner(System.in);

            System.out.print("\n\tEnter any number : ");
            num = Integer.parseInt(Sc.nextLine());

            try
            {
                if(num%2 != 0)
                    throw(new OddNumberException());    // Statement 2
                else
                    System.out.print("\n\t" + num + " is an even number");
            }
            catch(OddNumberException Ex)
            {
                System.out.print("\n\tError : " + Ex.getMessage());
            }

            System.out.print("\n\tEnd of program");
        }
    }

    Output:

            Enter any number : 47
            Error : Odd number exception
            End of program

OddNumberException class is derived from the Exception class. To implement user defined exception we need to throw an exception object explicitly.

In the above example, If the value of num variable is odd, then the throw keyword will raise the user defined exception and the catch block will get execute.

Example 2 of user defined PrimeNumerException class


//UserdefinedExceptionDemo

    import java.util.Scanner;

    class OddNumberException extends Exception  // Statement 1
    {
        OddNumberException()
        {
            super("Odd number exception");
        }
        OddNumberException(String msg)
        {
            super(msg);
        }
    }

    class UserdefinedExceptionDemo
    {
        public static void main(String[] args)
        {
            int num;

            Scanner Sc = new Scanner(System.in);

            System.out.print("\n\tEnter any number : ");
            num = Integer.parseInt(Sc.nextLine());

            try
            {
                if(num%2 != 0)
                    throw(new OddNumberException());    // Statement 2
                else
                    System.out.print("\n\t" + num + " is an even number");
            }
            catch(OddNumberException Ex)
            {
                System.out.print("\n\tError : " + Ex.getMessage());
            }

            System.out.print("\n\tEnd of program");
        }
    }

    Output:

            Enter any number : 47
            Error : Odd number exception
            End of program

OddNumberException class is derived from the Exception class. To implement user defined exception we need to throw an exception object explicitly.

In the above example, If the value of num variable is odd, then the throw keyword will raise the user defined exception and the catch block will get execute.

Advertisement