Java Program on Exception Handling Part – 2
Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java
Program 1
package beans;
import java.util.Scanner;
public class TestMain3
{
public static void main(String[] args)
{
int ar[]={10,20,30,40,50};
int i;
Scanner scan=new Scanner(System.in);
try
{
System.out.println("Enter Index Number");
i=scan.nextInt();
System.out.println("Array value " + ar[i]); //Exception
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(e);
}
try
{
int a,b,c;
System.out.println("Enter First Number");
a=scan.nextInt();
System.out.println("Enter Second Number");
b=scan.nextInt();
c=a/b; // Exception
System.out.println("Division is "+c);
}
catch(ArithmeticException e)
{
System.out.println(e);
}
System.out.println("..........Program END........");
}
}Program 2
package beans;
public class TestMain4
{
public static void main(String[] args)
{
int ar[]={10,20,30,40,50};
int i,b,c;
try
{
i=9;
b=0;
c=ar[i]/b; // Exception
System.out.println("Division is "+c);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(e);
}
catch(ArithmeticException e)
{
System.out.println(e);
}
System.out.println("..........Program END........");
}
}Program 3
class TestException
{
public static void main(String args[])
{
int a,b,c;
try
{
a= Integer.parseInt(args[0]);
b= Integer.parseInt(args[1]);
c=a/b;
System.out.println("Division is : " +c);
}
catch(Exception e)
{
System.out.println(e);
}
}
} Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google

