Throw Throws Finally Keyword in Java

Advertisements

Java Throw Keyword

Java throw keyword is used to throw an exception explicitly. The throw keyword must be in try block. When JVM encounters the throw keyword, it stops the execution of try block and jump to the corresponding catch block.

Example of throw keyword in java


//ThrowDemo.java

    class ThrowDemo
    {
        public static void main(String[] args)
        {

            int a=45,b=0,rs;

            try
            {
                if(b==0)        //Statement 1
                    throw(new ArithmeticException("Can't divide by zero."));
                else
                {
                    rs = a / b;
                    System.out.print("\n\tThe result is : " + rs);
                }
            }
            catch(ArithmeticException Ex)
            {
                System.out.print("\n\tError : " + Ex.getMessage());
            }

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

    Output:
                Error : Can't divide by zero.
                End of program.

In the above example, statement 1 is validating the value of b, if b is zero then, throw an exception otherwise show the result.

Java Throws Keyword

Every time, when an exception occur, try block throws an exception to corresponding catch block. When we need a method to throw an exception, we use throws keyword followed by method declaration.

When a method throws an exception, we must put the calling statement of method in try-catch block.

Example of throws keyword in java


//ThrowsDemo.java

    class ThrowsDemo
    {
        void Division() throws ArithmeticException
        {
            int a=45,b=0,rs;

            rs = a / b;
            System.out.print("\n\tThe result is : " + rs);

        }

        public static void main(String[] args)
        {

            ThrowsDemo T = new ThrowsDemo();

            try
            {
                T.Division();
            }
            catch(ArithmeticException Ex)
            {
                System.out.print("\n\tError : " + Ex.getMessage());
            }

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

    Output:
                Error : / by zero.
                End of program.

In the above example, division() method is throwing an ArithmeticException, therefore, we have written the calling statement of division() method in try-catch block.

Java Finally Keyword

Finally block is used to cleanup resources allocated by try block, such as closing file, closing database connection, etc. The catch block is executed when the exception occur in try block. The finally block is used to execute a given set of statements, whether an exception has occurred or not. We can have only one finally block for each try block. It is not mandatory to have a finally block after try-catch block.

Example of finally keyword in java


//FinallyBlockDemo.java

    class FinallyBlockDemo
    {
        public static void main(String[] args)
        {
            int a=45,b=0,rs=0;

            try
            {
                rs = a / b;
            }
            catch(ArithmeticException Ex)
            {
                System.out.print("\n\tError : " + Ex.getMessage());
            }
            finally
            {
                System.out.print("\n\tThe result is : " + rs);
            }
        }
    }

    output:

                Error : / by zero
                The result is : 0

Advertisement