Sunday, January 17, 2010

Exception Program in Java

/* Pass 2 parameters from command Line. First parameter should be an integer , second is also an integer.Try to make the situation, that if user does not pass any parameter, then, there should be a message like "invalid parameter due to ArrayIndexOutOfBoundException".Second parameter for an array Size.If Array Size is -ve then, NegativeArraySizeException will be thrown and there should be a message
"ArraySize should not be -ve". Serach an element which is the args[0] position element from an user created Array. If that element not found from Array then a message should be displayed "Element not Found " else "Element Found";
**************************************************************************************/


import java.io.*; //Predefined package for input at run time


class Exceptionsize
{



public void test(String args[]) throws IOException, ArrayIndexOutOfBoundsException
{


try
{
int x,y, len = 0;

x=Integer.parseInt(args[0]);

y=Integer.parseInt(args[1]);

System.out.println(" User Entered from command Line...");

System.out.println("*************************************");

System.out.print(x + " " +y);

int total = x + y;

System.out.println(" Sum of 2 parameters passed from Command Line is : " + total );

System.out.println("***********************************");

len = args.length;

if(len<2)
{
throw new ArrayIndexOutOfBoundsException();
}


int size, i;

size = Integer.parseInt(args[1]);

int Arr[];


Arr = new int[size];

System.out.println("*********************************");

if(size < 0)
{
throw new NegativeArraySizeException();
}



else
{
System.out.println("Array Size is not -ve ");
}

InputStreamReader in = new InputStreamReader(System.in);

BufferedReader br = new BufferedReader(in);


//BufferedReader br = new BufferedReader(new InputStreamReadern(System.in));

for(i = 0;i {
Arr[i] =Integer.parseInt(br.readLine());
}

System.out.println("*************************************");

System.out.println("Display Array Element...");

for(i=0;i {
System.out.print(" " + Arr[i]);
}

System.out.println();

System.out.println("Search that element which is the first parameter from command Line ");

System.out.println("*************************************");

boolean temp = false;

try
{

for(i = 0;i < size; i++)
{

if(Arr[i] == Integer.parseInt(args[0]))
{

System.out.println("***********");
System.out.print("Element Found");

temp = true;

break;
}

}

if(temp==false)
{

System.out.println("******************");
System.out.println(" Element not found ");

System.out.println("************");
}


}

catch(Exception e)
{


}
}

catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Pls enter correct number of Argumets !!! ");
}

catch(NegativeArraySizeException e)
{
System.out.println("Array size should not be Negative ! Pls take positive number for Array size");
}

}





public static void main(String args[])
{
Exceptionsize obj=new Exceptionsize();


try
{
obj.test(args);
}

catch(Exception e)
{

}


finally
{

System.out.println("I am in finally block");
}

}

}

No comments:

Post a Comment