class exceptiondemo1

{

public static void main(String as[])

{

System.out.println("exception handling program");

int a,b,add,sub,mul,div;

try

{

a=Integer.parseInt(as[0]);

b=Integer.parseInt(as[1]);

add=a+b;

sub=a-b;

mul=a*b;

div=a/b;

System.out.println("add: "+add+" sub : "+sub);

System.out.println("mul: "+mul+" div: "+div);

 }

catch(ArithmeticException e)

{

System.out.println(" division by zero");

}

catch(NumberFormatException e)

{

System.out.println(" string to number problem");

}

catch(ArrayIndexOutOfBoundsException e)

{

System.out.println(" accessing bad array index");

}

System.out.println(" program successfully runs..");

}

}

 

Output:

E:\java>javac exceptiondemo1.java

Input data set :1  E:\java>java exceptiondemo1 100 20

exception handling program

add: 120 sub : 80

mul: 2000 div: 5

 program successfully runs..

Input data set :2 E:\java>java exceptiondemo1 100 0

exception handling program

 division by zero

 program successfully runs..

 Input data set :3 E:\java>java exceptiondemo1 100 10a

exception handling program

 string to number problem

 program successfully runs..

 Input data set :4  E:\java>java exceptiondemo1 100

exception handling program

 accessing bad array index

 program successfully runs..