JAVA PROGRAM TO HANDLE EXCEPTION USING TRY AND MULTIPLE CATCH BLOCK.

 

package exception;

 import java.io.IOException;

public class Exception {

       public static void main(String[] args) throws IOException {

    try{ 

    int a=args.length;

    /* if No command line args are present,the following statement will generate a divide by zero exception*/

       int b=53/ a;

    System.out.println("a= "+a);

    try

        {

            // nested try block

            /* if one command line arg is used,then a divide by zero exception will be generated by the following code */

            if (a==1) a=a/(a-a);// division by zero

            /* if two command line args are used,then generate an out of bounds exception*/

            if (a==2){

                int c[]={1};

                c[42]=99;//generate an out of bounds exception

                }

        }catch(ArrayIndexOutOfBoundsException e){

            System.out.println("Array index out of bounds :"+e);

        }

    }catch(ArithmeticException e){

        System.out.println("Divide by 0:"+e);

     }

    }

}

OUTPUT 1:[FOR NO ARGUMENTS]






OUTPUT 2:[FOR 1 ARGUMENT PASSING THROUGH COMMAND LINE ARGUMENTS]






OUTPUT 3:[FOR 2 ARGUMENTS PASSING THROUGH COMMAND LINE ARGUMENTS]






OUTPUT 4:[FOR 3 ARGUMENTS PASSING THROUGH COMMAND LINE ARGUMENTS]








No comments:

Post a Comment