java | multiple catch example


//Example of multiple catch

import java.util.Random;

class Test
{

public static void main(String ar[])
{

try{
int a=Integer.parseInt(ar[0]);
int b=Integer.parseInt(ar[1]);//for array index out of bound exception
int c=a/b;      //for  divide by zero exception
System.out.println("c="+c);
}
catch(ArrayIndexOutOfBoundsException e)
{

System.out.println("Take proper number of elements from command line");

}


catch(ArithmeticException e)
{

System.out.println("don't divide number by zero");

}

catch(Exception e)//Exception is super class of all type of Exceptions
{

System.out.println("there is some another type of exception fired");

}
}

}
Reactions:

0 comments:

Post a Comment