Thursday, 8 September 2011

Exception handling in Java

import java.io.*;
class exc1
{
    public static void main(String arg[])throws IOException
      {
             DataInputStream d=new DataInputStream(System.in);
             int c,d1,d2,i,j,sum=0;
             double ans;
             int a[]=new int[5];
             int b[]=new int[5];
             try
             {
             System.out.print("Enter the Divedent=");
             d1=Integer.parseInt(d.readLine());
             System.out.print("Enter the divisor=");
             d2=Integer.parseInt(d.readLine());
             ans=d1/d2;
             System.out.println("The answer ="+ans);
           
             System.out.println("Enter the number in array one by one");
             for(i=0;i<5;i++)
             {
              a[i]=Integer.parseInt(d.readLine());
             }
             System.out.print("Enter which index number do you want to print=");
             c=Integer.parseInt(d.readLine());
             System.out.println(a[c]);
           
             System.out.println("Enter the Integer value one by one to Sum");
             for(j=0;j<5;j++)
             {
             b[j]=Integer.parseInt(d.readLine());
             }
             }
             catch(ArrayIndexOutOfBoundsException e )
              {
                System.out.println("ArrayIndexOutOfBoundsException");
              }
                        
              catch(ArithmeticException e1)
              {
                        System.out.print("Divided by Zero");
              }
                  
          catch(NumberFormatException e2)
          {
            System.out.println("NumberFormatException");
            for(j=0;j<5;j++)
            sum=sum+b[j];
            System.out.print("The Sum of remaining valid integer ="+sum);
          }
}
}

No comments:

Post a Comment