EXCEPTION
EXCEPTION
Output:
Java
interface
show() is a method which generates a checked
exception and handles the exception within
show() method
…………….
…………….
try
{
Statement; //generates an exception
}
catch (Exception-type e)
{
Statement; //processes the exception
}
……………
……………
Class error3
{
public static void main(String args[])
{
int a=10;
int b=5;
int c=5;
int x,y;
try
{
x=a/(b-c); //Exception here
}
catch(Exception e)
{
System.out.println(“Division by zero”);
}
y=a/(b+c);
System.out.println(“y=”+y);
}
}
Output:-Division by zero
y=1
class egnestedtry
{
public static void main(String args[])
{
try
{
int a=2,b=4,c=2,x=7,z;
int p[]={2};
p[3]=33;
try
{
Z=x/((b*b)-(4*a*c));
System.out.println(“The value of z is”=+z);
}
catch(ArithematicException e)
{
System.out.println(“Division by zero in
arithematic expression);
}
}
catch(ArrayIndexOutOfBoundException e)
{
System.out.println(“Array index is out-of-bond”
);
}
}
}
class error4
{
int a[]={5,10};
int b=5;
try
{
int x=a[2]/b-a[1];
}
catch(ArithematicException e)
}
System.out.println(“Division by zero”);
}
catch(ArrayIndexOfBoundsException e)
{
System.out.println(“Array index error”);
}
catch(ArrayStoreException e )
{
System.out.println(“Wrong data type”);
}
int y=a[1]/a[10];
System.out.println(“y=”+y);
}
}
Output:Array index error
y=2