7.1: Prgram
on try and catch.
class Error1
{
public
static void main(String s[])
{
int a=10, b=5, c=5;
int x,y;
try
{
X =a/ (b-c);
}
catch(ArithmeticException
e)
{
System.out.println(“Division
by zero”);
}
y=a/ (b+c);
system.out.println(“y=
”+ y);
}
}
Output is
Division by
zero
y=1
7.2: Program
on multiple catch statements.
class Error2
{
public static void
main(String s[ ])
{
Int a[ ] = {5,10};
Int b=10,x,y;
{
X=
a[2]/(b-a[1]);
}
catch(ArithmeticException
e)
{
System.out.println(“Division
by zero”);
}
catch(ArrayIndexOutOfBoundsException
e)
{
System.out.println(“Array
Index Error”);
}
y=a[1]/ a[0];
System.out.println(“y=”+ y);
}
}
Output
Array Index
Error
y=2
7.3:Program
on finally statement.
Class Error3
{
public
static void main(String s[ ])
{
int a[ ]={5,10};
int b=10,x,y;
try
{
X=a[2]/(b-a[1]);
}
catch(ArithmeticException
e)
{
System.out.println(“Division
by zero”);
}
Catch(ArrayIndexOutOfBoundsException
e)
{
System.out.println(“Array
Index Error”);
}
Finally
{
Y=a[1]/ a[0];
System.out.println(“y=”+y);
}
}
}
Output
Array Index Error
Y=2
7.4. Program
on throw statement.
Class Error4
{
Int a;
Void GetAge(int
z)
{
Try
{
If(z<18)
Throw
new ArithmeticException()
}
Catch(ArithmeticException
e)
{
System.out.println(“Wrong
data”);
}
}
}
Class
Student
{
Public
static void main(String s[])
{
Error4 obj=new Error4();
Obj.GetAge(15);
}
}
Output is
Wrong data
7.5: Program
on throws statement.
Class Error4
{
int a;
void GetAge(int z)
{
if(z<18)
throws new
ArithemeticExecption()
}
}
Class
student
{
Public
static void main(string s[] )
{
Error4 obj= new Error4();
Try
{
Obj.GetAge(15);
}
Catch(ArithmeticException
e)
{
System.out.println(“Wrong
data”);
}
}
}
}
Output is
Wrong data
0 Comments
Please share your opinions and suggestions with us.