Saturday 12 October 2013

Program to demonstrate multiple catch statements

//Program to demonstrate multiple catch statements
//Developed by Ahlam Ansari
import java.util.*;
   
class MultiCatch
{
    public static void main(String ahlam[])
    {
        int n;
        Scanner d=new Scanner(System.in);
        System.out.println("Enter the index");
        n=d.nextInt();
        int arrA[]=new int[n];
        try
        {
            for(int i=0;i<=n;i++)
            {   
                arrA[i]=i;
                if (i==2)
                {
                    arrA[i]=i/(i-2);
                }
            }
        }
        catch(ArithmeticException e)
        {
            System.out.println("ERROR:DIVIDE BY ZERO ERROR");
        }
        catch(ArrayIndexOutOfBoundsException e)
        {
            System.out.println("ERROR:INDEX OUT OF RANGE");
        }
    }
}

/*
Output
C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>javac MultiCatch.java


C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>java MultiCatch
Enter the index
5
ERROR:DIVIDE BY ZERO ERROR

C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>
*/

No comments:

Post a Comment